So Classes and Why do we need them
In Java it require that you write code in inside class but in Python it does not required.you can write a code inside a class but you don't have to.In Python we define a class using class keyword similar to define a function using def keyword.
Let's take an complete object-oriented example:
classess.py
students =[]
class Student:
school_name = "Abhinaw Demo"
def __init__(self,name,student_id=1):
self.name=name
self.student_id=student_id
students.append(self)
def __str__(self):
return "Student "+ self.name
def get_name(self):
return self.name.capitalize()
def get_school_name(self):
return self.school_name
class NewSchoolStudent(Student):
school_name="Inheritance Abhinaw Demo"
def get_school_name(self):
return "This is inheritance"
def get_name(self):
original_value= supper().get_name()
return original_value + "New School"
abhinaw= NewSchoolStudent("Abhinaw")
print(abhinaw.get_school_name())
print(abhinaw.get_name())
This demo code includes classes,adding methods to our classes,Constructor,Instance and Class Attributes and Inheritance and Polymorphism.
No comments:
Post a Comment