python类的继承、封装和多态


摘自https://www.cnblogs.com/evablogs/p/6724477.html

 

继承

1
2
3
4
5
6
7
8
class  Person( object ):
     def  __init__( self , name, gender):
         self .name  =  name
         self .gender  =  gender
class  Student(Person):
    def  __init__( self , name, gender,score):                         #score属性是子类自己的属性
         super (Student, self ).__init__(name,gender)                  #super(子类名称,self).__init__(继承父类的属性1,属性2):初始化父类,继承Person父类的name和gender属性
         self .score = score

除了从一个父类继承外,Python允许从多个父类继承,称为多重继承。

多重继承的目的是从两种继承树中分别选择并继承出子类,以便组合功能使用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#哪类人
class  Person( object ):
     pass
 
class  Student(Person):
     pass
 
class  Teacher(Person):
     pass
 
class  SkillMixin( object ):
     pass
 
#技能类
class  BasketballMixin(SkillMixin):
     def  skill( self ):
         return  'basketball'
 
class  FootballMixin(SkillMixin):
     def  skill( self ):
         return  'football'
 
#拥有哪种技能的人的类型
class  BStudent(Student, BasketballMixin):                                #既是学生,又会打篮球的人,即继承学生类,也继承篮球技能类,多重继承
     pass
 
class  FTeacher(Teacher, FootballMixin):
     pass
 
=  BStudent()
print  s.skill()
 
>>> basketball
 
=  FTeacher()
print  t.skill()
>>> football

 

多态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class  Person( object ):
      def  __init__( self ,name):
           self .name = name
      def  a( self ):                                                #Person父类的a方法
           return  'I am a Person,my name is %s'  % self .name
class  Student(Person):
       def  __init__( self ,name,age):
            super (Student, self ).__init__(name)
            self .age = age
       def  a( self ):                                               #Student子类的a方法
           return  'I am a Student,my name is %s'  % self .name   
class  Teacher(Person):
       def  __init__( self ,name,score):
            super (Teacher, self ).__init__(name)
            self .score = score
       def  a( self ):                                               #Teacher子类的a方法
           return  'I am a Teacher,my name is %s'  % self .name   
def  show_a(x):                                                   #定义一个方法,用于接收x参数,返回每个类实例对象相对应的方法
      print  a()
 
 
p = Person( 'Bob' )
s = Student( 'Alice' , 12 )
t = Teacher( 'Lily' , 80 )
 
 
结果:
>>> show_a(p)
I am a Person,my name  is  Bob
 
>>> show_a(s)
I am a Student,my name  is  Alice
 
>>> show_a(t)
I am a Teacher,my name  is  Lily
 
结果返回子类自己的方法,但当子类的方法找不到时会顺着子类找到父类相对应的方法

 封装

将细节封装起来提供一个接口被访问,有效地保证了细节的安全。

1
2
3
4
5
6
7
8
9
class  Person( object ):
       def  __init__( self ):
            self .__name = 'a'
      @property                                    #使用@property将一个方法name变成属性,可以直接.name访问
       def  name( self ):                             #封装self.__name属性
            return  self .__name                   
 
p1 = Person()
p1.name                                           #p1.name可以直接访问name方法

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM