Home  >  Article  >  Backend Development  >  python learning-----class encapsulation, inheritance, polymorphism

python learning-----class encapsulation, inheritance, polymorphism

巴扎黑
巴扎黑Original
2017-06-23 14:48:201392browse

Encapsulation

Encapsulation is best understood. Encapsulation is one of the characteristics of object-oriented and the main characteristic of the concepts of objects and classes.

Encapsulation, that is, encapsulating objective things into abstract classes, and the class can only allow trusted classes or objects to operate its data and methods, and hide information from untrusted ones.

 1 class dog(object): 2     nationality='ch'#公有属性 3     def __init__(self,name,food,leven):#构造函数,方法,初始化方法 4         self.name=name #实例指针,指向 属性  对象成员 5         self.food=food 6         self.leven=leven 7         self.__haot='hhh'#前面双下划线定义为私有属性 8  9     def get_hoat(self):#定义方法为私有属性提供接口10         return self.__haot11     def say(self):#类中的方法  都是公有方法12         print('hello,my name is ',self.name)13     def eat(self,foods):14         print("my food is %s,but my eat %s"%(self.food,foods))15     def leve(self):16         print("my leve is ",self.leven)17 18     def __del__(self):19         print("删除中...")20 21 22 d=dog("liili",'gl',"5")23 d.say()24 d.eat('kkk')25 d.leve()26 print(d.get_hoat())#通用接口访问私有属性27 28 print(d._dog__haot)#强制访问私有属性29 print(d.nationality)30 31 dog.nationality='chan'32 print(d.nationality)33 d.nationality='us'34 print(d.nationality)
View Code

 1 class F1(object): 2     def __init__(self,n): 3         self.N = n 4         print('F1') 5  6 class F2(object): 7     def __init__(self,arg1): 8         self.a = arg1 9         print('F2')10 11 class F3(object):12     def __init__(self,arg2):13         self.b = arg214         print('F3')15 16 17 18 c1=F1('yjj')19 c2=F2(c1)#可以封装一个对象20 c3=F3(c2)#可以封装多层的对象21 print(c3.b.a.N)#通过  .  调用

Inheritance

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

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

The inherited class is called the "base class", "parent class" or "super class".

 1 class studen(object):#定义类  学生 基类 2     def __init__(self,name,age,clas):#名字,年龄,班级 3         self.name=name 4         self.age=age 5         self.clas=clas 6     def talk(self): 7         print('%stalk one.....'%self.name) 8     def walk(self): 9         print('%s walk....'%self.name)10     def info_user(self):11         print('name is %s, age is %s,clas is  %s'%(self.name,self.age,self.clas))12 13 class clas_one(studen):#继承studen14     def __init__(self,name,age,clas,score):#重构构造方法15         #studen.__init__(self,name,age,clas)#先继承, 再重构16         super(clas_one,self).__init__(name,age,clas)#新式类17         self.score=score#增加新对象成员18     def talk(self):#重写方法19         print('is new talk ,%s'%self.name)20     def score_info(self):#新增加 子类方法21         print(self.score,'分')22 23 p=clas_one('学生一',36,'一年三班',178)24 p.talk()25 p.score_info()
View Code
 1 class F1(object): 2     def __init__(self): 3         print('F1') 4     def a1(self): 5         print('F1a1') 6     def a2(self): 7         print('F1a2') 8  9 class F2(F1):10     def __init__(self):11         print('F2')12     def a1(self):13         self.a2()14         print('F2a1')15     def a2(self):16         print('F2a2')17 18 class F3(F2):19     def __init__(self):20         print('F3')21     def a11(self):22         print('F3a1')23     def a2(self):24         print('F3a2')25 26 obj=F3()27 obj.a1()#调用时,self指向当前对象

Polymorphism

##
 1 class Animal(object): 2     def __init__(self,name): 3         self.name=name 4     def talk(self): 5         raise NotImplementedError('提示出错') 6  7  8 class c(Animal):#继承Animal 9     def talk(self):10         print('%s 1111'%self.name)11 12 class d(Animal):#继承Animal13     def talk(self):14         print('%s 2222'%self.name)15 16 17 def talk_all(obj):#用函数来模拟多态18     obj.talk()19 20 c1=c('猫')21 d1=d("狗")22 23 talk_all(c1)24 talk_all(d1)
View Code

The above is the detailed content of python learning-----class encapsulation, inheritance, polymorphism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Use of Django AjaxNext article:Use of Django Ajax