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