Python 31st Day-----Class of encapsulation, inheritance, polymorphism .....

Source: Internet
Author: User

Packaging

The package is best understood. Encapsulation is one of the object-oriented features and is the main feature of object and class concepts.

Encapsulation, which is the encapsulation of objective things into abstract classes, and classes can put their own data and methods only trusted class or object operation, to the untrusted information hiding.

1 classDog (object):2nationality='CH'#Public Properties3     def __init__(Self,name,food,leven):#constructors, methods, initialization methods4Self.name=name#instance pointers, pointing to Property object Members5Self.food= Food6self.leven=Leven7Self.__haot='HHH'#Front double underline defined as private property8 9     defGet_hoat (self):#defines a method to provide an interface for a private propertyTen         returnSelf.__haot One     defSay (self):#methods in a class are public methods A         Print('hello,my name is', Self.name) -     defEat (self,foods): -         Print("My food is%s,but my eat%s"%(self.food,foods)) the     defleve (self): -         Print("my leve is", Self.leven) -  -     def __del__(self): +         Print("Delete in ...") -  +  AD=dog ("Liili",'GL',"5") at D.say () -D.eat ('KKK') - D.leve () - Print(D.get_hoat ())#Universal Interface access to private properties -  - Print(D._DOG__HAOT)#force access to private properties in Print(d.nationality) -  todog.nationality='Chan' + Print(d.nationality) -d.nationality='US' the Print(d.nationality)
View Code

Inherited

Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class.

New classes created through inheritance are called "subclasses" or "derived classes."

The inherited class is called the base class, the parent class, or the superclass.

1 classStuden (object):#Define class student base class2     def __init__(Self,name,age,clas):#name, age, Class3Self.name=name4Self.age= Age5self.clas=Clas6     defTalk (self):7         Print('%stalk one ... ..'%self.name)8     defWalk (self):9         Print('%s walk ....'%self.name)Ten     defInfo_user (self): One         Print('name is%s,%s,clas is%s'%(Self.name,self.age,self.clas)) A  - classClas_one (Studen):#Inherit Studen -     def __init__(Self,name,age,clas,score):#Refactoring Construction Method the         #studen.__init__ (Self,name,age,clas) #先继承, re-construct -Super (Clas_one,self).__init__(Name,age,clas)#New Class -Self.score=score#adding new Object Members -     defTalk (self):#overriding Method +         Print('is new talk,%s'%self.name) -     defScore_info (self):#new method of adding subclasses +         Print(Self.score,'points') A  atP=clas_one ('students a', 36,'Three shifts a year', 178) - P.talk () -P.score_info ()
View Code

Polymorphic

1 classAnimal (object):2     def __init__(self,name):3Self.name=name4     defTalk (self):5         RaiseNotimplementederror ('Prompt Error')6 7 8 classC (Animal):#Inherit Animal9     defTalk (self):Ten         Print('%s 1111'%self.name) One  A classD (Animal):#Inherit Animal -     defTalk (self): -         Print('%s 2222'%self.name) the  -  - defTalk_all (obj):#using functions to simulate polymorphic - Obj.talk () +  -C1=c ('Cat') +D1=d ("Dog") A  at Talk_all (C1) -Talk_all (D1)
View Code

Python 31st Day-----Class of encapsulation, inheritance, polymorphism .....

Related Article

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.