Three main features of Python object-oriented: encapsulation, inheritance, and polymorphism (example)

Source: Internet
Author: User
Tags print print

Inherited

Single Inheritance:

#类定义class people:    #定义基本属性    name = ' Age    = 0    #定义私有属性, private properties cannot be accessed directly outside the class    __weight = 0    #定义构造方法    def __init__ (self,n,a,w):        self.name = n        self.age = a        self.__weight = w    def speak (self):        Print ("%s" says: I'm%d years old.) "% (self.name,self.age)) #单继承示例class Student (people):    grade ="    def __init__ (self,n,a,w,g):        #调用父类的构函        people.__init__ (self,n,a,w)        self.grade = g    #覆写父类的方法    def speak (self):        print ("%s" said: I'm%d years old, I'm reading grade%d "% (Self.name,self.age,self.grade))   s = student (' Ken ', 10,60,3) S.speak ()

Achieve Results

Ken says: I'm 10 years old, I'm in Grade 3.

Multiple Inheritance: (although it is possible, but not recommended, you only need to know the order of inheritance is left to right)

#类定义class people: #定义基本属性 name = ' age = 0 #定义私有属性, private properties cannot be accessed directly outside the class __weight = 0 #定义构造方法 def __init_  _ (self,n,a,w): Self.name = n Self.age = a self.__weight = W def speak (self): print ("%s" says: I %d years old.        "% (self.name,self.age)) #单继承示例class Student (people): Grade =" Def __init__ (self,n,a,w,g): #调用父类的构函 people.__init__ (self,n,a,w) Self.grade = G #覆写父类的方法 def speak (self): print ("%s said: I'm%d years old, I'm reading grade%d"% (        Self.name,self.age,self.grade)) #另一个类, the preparation of multiple inheritance before class Speaker (): topic = ' name = ' def __init__ (self,n,t):  Self.name = n Self.topic = t def speak (self): print ("My name is%s, I am an orator, I am speaking the subject is%s"% (Self.name,self.topic))        #多重继承class sample (speaker,student): a = "def __init__ (self,n,a,w,g,t): student.__init__ (SELF,N,A,W,G) speaker.__init__ (self,n,t) test = sample ("Tim", 25,80,4, "Python") Test.speak () #方法名同, the default is to call the method of the parent class before the row in parentheses

Package:

Class Student (object):    def __init__ (self, Name, score):        self.name = name         Self.score = Scoremay = Student (" May ",")                      # Two attributes are required Peter = Student ("Peter", "May.name") print (Peter.name, peter.score) def Print_ Score (Student):                    # External function    Print_score (Student) # print ("%s ' s score is:%d"% (Student.name,student.score))             # Plain Print print ("{0} ' s score is: {1}". Format (Student.name,student.score))        Print_score (May)    Print_score ( Peter)

Polymorphic :

Class Animal (object):    def __init__ (self, name):  # Constructor of the class        Self.name = Name    def talk ( Self):        raise Notimplementederror ("subclass must implement abstract method") class Cat (Animal):    def Talk (self):        print ('%s: Meow meow! '%self.name) class Dog (Animal):    def Talk (self):        print ('%s: Wang! Wang! Wang! '%self.name) def func (obj): #一个接口, multiple morphology    obj.talk () c1 = Cat (' fluffy ') D1 = Dog (' Gray ') func (C1) func (D1)

Three main features of Python object-oriented: encapsulation, inheritance, and polymorphism (example)

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.