Python類的繼承(進階5)


Python類的繼承(進階5)

 

1. python中什么是繼承

  python中什么是繼承:

  • 新類不必從頭編寫
  • 新類從現有的類繼承,就自動擁有了現有類的所有功能
  • 新類只需要編寫現有類缺少的新功能

  繼承的好處:

  • 復用已有代碼
  • 自動擁有了現有類的所有功能
  • 只需要編寫缺少的新功能

  繼承的特點:

  • 子類和父類是is關系

  python繼承的特點:

  • 總是從某個類繼承
  • 不要忘記調用super().init

2. python中繼承一個類

 1 class Person(object):
 2     def __init__(self, name, gender):
 3         self.name = name
 4         self.gender = gender
 5 class Teacher(Person):
 6     def __init__(self, name, gender, course):
 7         super(Teacher, self).__init__(name, gender)
 8         self.course = course
 9 
10 t = Teacher('Alice', 'Female', 'English')
11 print t.name
12 print t.course

 

3. python中判斷類型

  函數isinstance()可以判斷一個變量的類型,既可以用在Python內置的數據類型如str、list、dict,也可以用在我們自定義的類,它們本質上都是數據類型。

 1 class Person(object):
 2     def __init__(self, name, gender):
 3         self.name = name
 4         self.gender = gender
 5 
 6 class Student(Person):
 7     def __init__(self, name, gender, score):
 8         super(Student, self).__init__(name, gender)
 9         self.score = score
10 
11 class Teacher(Person):
12     def __init__(self, name, gender, course):
13         super(Teacher, self).__init__(name, gender)
14         self.course = course
15 
16 t = Teacher('Alice', 'Female', 'English')
17 
18 print isinstance(t, Person)
19 print isinstance(t, Student)
20 print isinstance(t, Teacher)
21 print isinstance(t, object)

 

4. python中多態

 1 class Person(object):
 2     def __init__(self, name, gender):
 3         self.name = name
 4         self.gender = gender
 5     def whoAmI(self):
 6         return 'I am a Person, my name is %s' % self.name
 7 
 8 class Student(Person):
 9     def __init__(self, name, gender, score):
10         super(Student, self).__init__(name, gender)
11         self.score = score
12     def whoAmI(self):
13         return 'I am a Student, my name is %s' % self.name
14 
15 class Teacher(Person):
16     def __init__(self, name, gender, course):
17         super(Teacher, self).__init__(name, gender)
18         self.course = course
19     def whoAmI(self):
20         return 'I am a Teacher, my name is %s' % self.name
21         
22         
23 import json
24 
25 class Students(object):
26     def read(self):
27         return r'["Tim", "Bob", "Alice"]'
28 
29 s = Students()
30 
31 print json.load(s)

 

5. python中多重繼承

  除了從一個父類繼承外,Python允許從多個父類繼承,稱為多重繼承。Java不能多繼承

 1 class A(object):
 2     def __init__(self, a):
 3         print 'init A...'
 4         self.a = a
 5 
 6 class B(A):
 7     def __init__(self, a):
 8         super(B, self).__init__(a)
 9         print 'init B...'
10 
11 class C(A):
12     def __init__(self, a):
13         super(C, self).__init__(a)
14         print 'init C...'
15 
16 class D(B, C):
17     def __init__(self, a):
18         super(D, self).__init__(a)
19         print 'init D...'
20         
21         
22 class Person(object):
23     pass
24 
25 class Student(Person):
26     pass
27 
28 class Teacher(Person):
29     pass
30 
31 class SkillMixin(object):
32     pass
33 
34 class BasketballMixin(SkillMixin):
35     def skill(self):
36         return 'basketball'
37 
38 class FootballMixin(SkillMixin):
39     def skill(self):
40         return 'football'
41 
42 class BStudent(BasketballMixin):
43     pass
44 
45 class FTeacher(FootballMixin):
46     pass
47 
48 s = BStudent()
49 print s.skill()
50 
51 t = FTeacher()
52 print t.skill()

 

6. python中獲取對象信息

  除了用 isinstance() 判斷它是否是某種類型的實例外,還有沒有別的方法獲取到更多的信息呢?

  首先可以用 type() 函數獲取變量的類型,它返回一個 Type 對象

  dir() 函數獲取變量的所有屬性

  dir()返回的屬性是字符串列表,如果已知一個屬性名稱,要獲取或者設置對象的屬性,就需要用 getattr() 和 setattr( )函數了

 1 class Person(object):
 2     def __init__(self, name, gender):
 3         self.name = name
 4         self.gender = gender
 5 
 6 class Student(Person):
 7     def __init__(self, name, gender, score):
 8         super(Student, self).__init__(name, gender)
 9         self.score = score
10     def whoAmI(self):
11         return 'I am a Student, my name is %s' % self.name
12 
13 print type(123) # <type 'int'>
14 
15 s = Student('Bob', 'Male', 88)
16 print s  # <class '__main__.Student'>
17 
18 print dir(123) # ['__abs__', '__add__', '__and__', '__class__', '__cmp__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__float__', '__floordiv__', '__format__', '__getattribute__', '__getnewargs__', '__hash__', '__hex__', '__index__', '__init__', '__int__', '__invert__', '__long__', '__lshift__', '__mod__', '__mul__', '__neg__', '__new__', '__nonzero__', '__oct__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'bit_length', 'conjugate', 'denominator', 'imag', 'numerator', 'real']
19 
20 print dir(s) # ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'gender', 'name', 'score', 'whoAmI']
21 
22 print getattr(s, 'name') # Bob
23 setattr(s, 'name', 'Adam') 
24 print s.name # Adam
25 
26 class Person(object):
27 
28     def __init__(self, name, gender, **kw):
29         self.name = name
30         self.gender = gender
31         for k, v in kw.iteritems():
32             setattr(self, k, v)
33 
34 
35 p = Person('Bob', 'Male', age=18, course='Python')
36 print p.age # 18
37 print p.course #Python


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM