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