This is my code:
class Person:
def __init__(self, name):
self.name = name
class Student(Person):
def register(self, school):
pass
def payfee(self, money):
pass
def chooseClassAndGrand(self, obj):
pass
class Teacher(Person):
pass
I want to add a class
instance property to the Student class, how to do with that in the Student class code, if I do not want to rewrite the __init__()
method?
You do not need to rewrite __init__
. Assuming you want Person
's __init__
functionality to be invoked when creating an instance of Student
, you may use the super
keyword inside the __init__
function of Student
:
class Student(Person):
def __init__(self):
super().__init__() # python3.0+
self.classAndGrade = ...
...
If you're on python < 3.0, you can use
super(Person, self).__init__()
This is the easiest way to do it.
Do add to COLDSPEED, you can also use the below to add attributes:
class Student(Person):
def __init__(self, name, classAndGrade):
Person.__init__(self, name)
self.classAndGrade = classAndGrade
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.