Python Object-oriented (2)

Source: Internet
Author: User

1, in addition to encapsulation, Python object-oriented and inherit this function, the following code, which is a simple inheritance function.

Class Animal:    def chi (self):        print (self.name+ ' eat ')    def He (self):        print (self.name + ' drink ') class Cat:    def __init__ (self, name):        self.name = name    def jiao (self):        print (' aim ') class Dog (Animal): # Inherit animal the parent class, with the method    def __init__ (self, name) for the parent class:        self.name = name    def jiao (self):        print (self.name+ ' Wang ') Jay = Dog (' name ') Jay.chi () #这里就可以调用父类里面的方法

If both the subclass and the parent have the same method, the subclass's method is executed by default

/2,

2, Python can also inherit multiple parent classes at the same time, the priority of the inheritance method is to find their own method, and then first left and right

#Python里面一个类可以同时继承多个类, java,c# is not allowed. Class Animal:    def chi (self):        print (self.name+ ' eat ')    def she (self):        Print (Self.name + ' love drink ') class God:    def shit (self):        print (' Shit ')    def she:        print (' Yellow gambling poison ') # Class cat:#     def __init__ (self, name): #         self.name = name#     def jiao (self): #         print (' aim ') class Dog ( Animal,god):    "If the method repeats, take precedence from yourself here, if the method is repeated in two parent classes    such as the She method here, here the default is the left parent class first, in the left parent class to find the    she method, No more going to the next parent class looking for    "    def __init__ (self, name):        self.name = name    def he (self):        print (Self.name + ' Do not love to drink ')    def jiao (self): print        (self.name+ ' Wang ') jay = Dog (' Jay ') jay.she () #多个父类的方法继承是先左后右, here will print out Jay loves to drink

In the inheritance of the class of Python3, it is the depth first, as the following code

Class Jay (object):    def F1 (self):        print (' Jay '), Class A (Jay):    def f (self):        print (' A ') class B (Jay):    def F1 (self):        print (' B ') class C (A):    def f (self):        print (' C ') class D (B):    def f (self):        print ( ' D ') class E (c,d):    def f (self):        print (' e ') obj = e () "In the inheritance of the Python class is the depth first, here the method of calling F1, will first look inside the E itself, Then in the Class C search, or can not find the words will be in the C of the parent class A class inside the search, but also can not find on the turn to the D-class inside the search, where a class B has inherited Jay this base class, but finally will go to Jay inside look for F1 this method, That is, both D and B cannot find the F1 method before going to the Jay class to look for "obj.f1" () #这里输入结果是B

Order

/3

3, polymorphic, Python itself supports polymorphism

#多态, is a variety of forms class Foo:    def F1 (self):        print (' Foo ') class Bar:    def F1 (self):        print (' Bar ') def func ( ARG): #这里显示arg的多种形态, can be an Foo class object, or it can be an object of the Bar class    arg.f1 () F=foo () E=bar () func (f) #默认参数给的什么就是什么, without error func (e)

  

4. If both subclasses and parent classes have constructors, there are two ways to inherit

Class A:    def __init__ (self):        print (' A construction method ')        self.name= ' Jia Peng '        self.height = ' 1.65 ' class B (a):    def __init__ (self):        print (' construction method of ' B ')        self.age = [        b,self]. __init__ () #先执行父类的构造方法, then execute yourself B =b () # So B also has the properties of a print (b.__dict__)

Results

There's another way to do that.

Class A:    def __init__ (self):        print (' A's construction method ')        self.name= ' Jia Peng '        self.height = ' 1.65 ' class B (a):    def __init__ (self):        print (' construction method of B ')        self.age =        a.__init__ (self) #执行父类的构造函数, passing an instance of B to B =b () # So B also has the properties of a print (b.__dict__)

The result is the same.

Python Object-oriented (2)

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.