Python Growth Notes-Basics (vii) Python object-oriented

Source: Internet
Author: User

  Three major characteristics: 1, Encapsulation: In the class to the data assignment, internal calls to the external user is transparent, which makes the class into a capsule or container, the bread containing the class data and methods 2, inheritance: A class can derive subclasses, the properties defined in the parent class, methods will automatically inherit the class # class People: #经典类class People (object): #新式类def __init__ (self,,age): Self.name = Self.age = Age def eat (self):p rint ('%s ') Eating '%self.name)  def work (self):p rint ("%s are work hard"%self.name)   class Man (People): # Subclass inherits People parent Def __init__ (self,name,age,sex): #People. __init__ (self, name, age) Super (man, self). __INIT__ (name, age) # New self.sex = Sex def drink (self):p rint ("%s was drinking"%self.sex)  class Woman (people):d EF get_brith (self): Print ("%s is get"% self.name)  def eat (self):P eople.eat (self)   m1 = Man ("Mmmmm", 23, "male") m1.eat () M1.drink ()  w1 =woman ("Wwwww", $) W1.get_brith () W1.eat () # class people: #经典类class people (object): #新式类def __init__ (self,,age): Self.name = Self.age = Age def eat (self):p rint ('%s was eating '%self.name)  def work (self):p rint ("% S is work hard "%self.name)   class Man (people): #子类继承People父类def __init__ (Self,naMe,age,sex): #People. __init__ (self, name, age) Super (Mans, Self). __INIT__ (name, age) #新式self. Sex = Sex def drink ( Self):p rint ('%s is drinking '%self.sex)  class Woman (people):d EF get_brith (self):p rint ('%s is get '% self.name)  def eat (self):P eople.eat (self)   m1 = Man ("Mmmmm", 23, "male") M1.eat () M1.drink ()  w1 =woman ("Wwwww", W1.get_brith () w1.eat ()  3, polymorphism: One interface, multiple implementations: interface reuse        Syntax: calling functions-->  Method Construction Method: Contrary to the constructor, it is executed automatically when the instance is disposed, destroyed, and is usually used to do some finishing work, such as closing a database link or closing some temporary files   private methods: Def __eat ():    Two kinds of query strategies; Breadth First: Python 3.x Python 2.x's new class depth priority: Python 2.x classic class is a depth-first query strategy       

Python Growth notes-Basic (vii) Python object-oriented

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.