Class inheritance and derivation for Python

Source: Internet
Author: User
Tags instance method

I. Introduction to Inheritance and derivation:

In fact, it is a thing that stands at a different point of view, and plainly is to define a new class based on one or several classes. For example, the definition of animals and then the derivation of human beings, you can also say that humans inherit the animal class. a meaning. In addition, Python is similar to C and C + + support multiple inheritance, a class can inherit multiple classes, such as mom and dad gave birth to children, children inherit the properties and methods of Mom and dad, and so on. In addition, because all classes in Python3 are new classes, Python2 can also define modern classes by inheriting object, so governance is introduced in the new class syntax, and for compatibility with Python2 and Python3, the individual recommendation uses the new class.

Ii. How to define the inheritance and multiple inheritance of classes

1. Defining inheritance and multiple inheritance of classes

1.1 Inheritance

1 classPerson (object):2     def __init__(self,name,age,gender,pid):3Self.name =name4Self.age = Age5Self.gender =Gender6Self.pid =PID7         Print("Create Person")8     defShowName (self):9         Print("name:%s"%self.name)Ten  One classStudent (person): A     def __init__(self,name,age,gender,pid,sid,level,school): -Super (Student,self).__init__(name,age,gender,pid) -Self.sid =Sid theSelf.level = Level -Self.school =School -         Print("Create Student") -  +     defShowsid (self): -         Print("studentid:%s"%SELF.SID)

1.2 Multiple inheritance

1 classfather (object):2     def __init__(Self,name,money):3Self._fname =name4Self.fmoney = Money5 6 classmother (object):7     def __init__(Self,name,money):8Self._mname =name9Self.mmoney = MoneyTen  One classBaby (father,mother): A     def __init__(self,n0,n1,n2,m1,m2,): -Self.name =N0 -Father.__init__(SELF,N1,M1) theMother.__init__(SELF,N2,M2) -Self.money = Self.fmoney + Self.mmoney

III. inheritance constraints for variables and attributes:

1, the property of the instance and the method inheritance requirements:

#实例 inheritance of properties and methods: """ 1, ordinary instance variable can inherit, protection instance (single underline) variable can inherit, hidden instance (double underscore) variable is not inheritable. 2, the ordinary instance method can inherit, the protection instance (single underline) method can inherit, the hidden instance (double underline) method cannot inherit. """

2. The inheritance of class attributes and methods requires:

Note: A class variable is a global variable for all instances of the class and the class itself, and the class variable is modified, and the property invocation variable is modified. The instance variables are independent of each other. Class properties and methods, which can be called by an instance.

# class Properties and method inheritance: """ 1, ordinary class variables can inherit, protection class (single underline) variable can inherit, hidden class (double underscore) variable is not inheritable. 2, ordinary class method can inherit, protection class (single underline) method can inherit, hidden class (double underline) method is not inheritable. """

3. Inheritance of static methods:

The inheritance constraints of a static method are exactly the same as the first two, and both classes and instances can be called.

4. Calls to instances, classes, static methods, or properties of the parent class:

It can be called directly after inheritance, as long as the process constraints are met.

Iv. overloaded Parent class methods:

1. After the subclass overloads the parent class (class, instance, static) methods, the subclass and subclass objects are executed according to the newly defined (class, instance, static) method. The parent class and its instance remain unchanged, preserving the original method execution.

1 classPerson (object):2Check =03     def __init__(self,name,age,gender,pid):4Self.name =name5Self.age = Age6Self.gender =Gender7Self.pid =PID8         Print("Create Person")9 @classmethodTen     def __showcheck(CLS): One         Print(Cls.check) A     defShowid (self): -         Print("personid:%s"%self.pid) - @staticmethod the     defABCD (): -         Print "ABCD" -  - classStudent (person): +     def __init__(self,name,age,gender,pid,sid,level,school): -Super (Student,self).__init__(name,age,gender,pid) +Self.sid =Sid ASelf.level = Level atSelf.school =School -         Print("Create Student") -  -     defShowid (self): -         Print "********************************" -         Print("personid:%s"%self.pid) in         Print("studentid:%s"%self.sid) -         Print "********************************"

Class inheritance and derivation for Python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.