Python class inheritance usage instance analysis, python usage instance analysis
This example describes how to use python class inheritance. Share it with you for your reference. The specific method is as follows:
#!/usr/bin/python# Filename: inherit.pyclass SchoolMember: '''Represents any school member.''' def __init__(self, name, age): self.name = name self.age = age print'(Initialized SchoolMember: %s)'% self.name def tell(self): '''Tell my details.''' print'Name:"%s" Age:"%s"'% (self.name, self.age),class Teacher(SchoolMember): '''Represents a teacher.''' def __init__(self, name, age, salary): SchoolMember.__init__(self, name, age) self.salary = salary print'(Initialized Teacher: %s)'% self.name def tell(self): SchoolMember.tell(self) print'Salary: "%d"'% self.salaryclass Student(SchoolMember): '''Represents a student.''' def __init__(self, name, age, marks): SchoolMember.__init__(self, name, age) self.marks = marks print'(Initialized Student: %s)'% self.name def tell(self): SchoolMember.tell(self) print'Marks: "%d"'% self.markst = Teacher('Mrs. Shrividya',40,30000)s = Student('Swaroop',22,75)members = [t, s]for member in members: member.tell()# works for both Teachers and Students
The output result is as follows:
(Initialized SchoolMember: Mrs. Shrividya)(Initialized Teacher: Mrs. Shrividya)(Initialized SchoolMember: Swaroop)(Initialized Student: Swaroop)Name:"Mrs. Shrividya" Age:"40" Salary: "30000"Name:"Swaroop" Age:"22" Marks: "75"
How it works
To use inheritance, we use the basic class name as a tuples after the class name when defining the class. Then, we noticed that the _ init _ method of the basic class is called using the self variable, so that we can initialize the basic class part of the object. This is important at 01:10 -- Python does not automatically call the constructor of the basic class. You have to call it in person.
We also observe that we add the class name prefix before the method call, and then pass the self variable and other parameters to it.
Note:When we use the tell method of the SchoolMember class, we only use the instance of Teacher and Student as the instance of SchoolMember.
In addition, in this example, we call the tell method of the child type, instead of the tell method of the SchoolMember class. It can be understood that Python always finds the corresponding type of method first, which is the case in this example. If it cannot find the corresponding method in the export class, it will start to search for it one by one in the basic class. The basic class is specified in the tuples when the class is defined.
Annotation of a term-if more than one class is listed in the inheritance tuples, it is called multi-inheritance.
I hope this article will help you with Python programming.
Who can use the popular method to inherit the python class? Tell the younger brother about the inheritance of the class.
A general example is:
There is already a class called bird
Its flying methods/functions
Then you implement another sparrow class.
If no class is inherited
Then you need:
First implement the Flying Method
Then other methods (and attributes) specific to Sparrow are implemented separately)
This method is similar to the method of flying. It is obvious that it is a common method used by all species of birds.
Therefore, to avoid this, every other bird and every other bird must implement this method separately.
Therefore, we get a base class, that is, the basic class, the main class.
It implements some common things that everyone shares.
Including many methods and many attributes
Then the other child classes
After this base class is integrated
You don't have to repeat and reimplement the basic methods and attributes.
You only need to implement the unique features of your class.
I don't know if you understand it.
For more information, see my summary:
[Arrangement] basic object-oriented knowledge: Class, Object, and Instance)
(No post address is provided here. You can find the post address by searching the post title on google)
In python, how does one call the methods of the base class in the inheritance class?
Where is the _ init _ constructor. What do you want to call the parent class method. Take Class --..
Give you a comprehensive
Class Q (object ):
Def _ init _ (self ):
Self. x = False
Self. y = True
>>> Q = Q ()
>>> Class W (Q ):
Def _ init _ (self ):
# In this way, the x and y attributes of the parent class Q are inherited.
Q. _ init _ (self)
>>> W = W ()
>>> Print w. x, w. y