Python类的继承(进阶5)
2017-02-19 18:18
274 查看
Python类的继承(进阶5)
1. python中什么是继承
python中什么是继承:新类不必从头编写
新类从现有的类继承,就自动拥有了现有类的所有功能
新类只需要编写现有类缺少的新功能
继承的好处:
复用已有代码
自动拥有了现有类的所有功能
只需要编写缺少的新功能
继承的特点:
子类和父类是is关系
python继承的特点:
总是从某个类继承
不要忘记调用super().init
2. python中继承一个类
class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender class Teacher(Person): def __init__(self, name, gender, course): super(Teacher, self).__init__(name, gender) self.course = course t = Teacher('Alice', 'Female', 'English') print t.name print t.course
3. python中判断类型
函数isinstance()可以判断一个变量的类型,既可以用在Python内置的数据类型如str、list、dict,也可以用在我们自定义的类,它们本质上都是数据类型。class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender class Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = score class Teacher(Person): def __init__(self, name, gender, course): super(Teacher, self).__init__(name, gender) self.course = course t = Teacher('Alice', 'Female', 'English') print isinstance(t, Person) print isinstance(t, Student) print isinstance(t, Teacher) print isinstance(t, object)
4. python中多态
class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender def whoAmI(self): return 'I am a Person, my name is %s' % self.name class Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = score def whoAmI(self): return 'I am a Student, my name is %s' % self.name class Teacher(Person): def __init__(self, name, gender, course): super(Teacher, self).__init__(name, gender) self.course = course def whoAmI(self): return 'I am a Teacher, my name is %s' % self.name import json class Students(object): def read(self): return r'["Tim", "Bob", "Alice"]' s = Students() print json.load(s)
5. python中多重继承
除了从一个父类继承外,Python允许从多个父类继承,称为多重继承。Java不能多继承class A(object): def __init__(self, a): print 'init A...' self.a = a class B(A): def __init__(self, a): super(B, self).__init__(a) print 'init B...' class C(A): def __init__(self, a): super(C, self).__init__(a) print 'init C...' class D(B, C): def __init__(self, a): super(D, self).__init__(a) print 'init D...' 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(BasketballMixin): pass class FTeacher(FootballMixin): pass s = BStudent() print s.skill() t = FTeacher() print t.skill()
6. python中获取对象信息
除了用 isinstance() 判断它是否是某种类型的实例外,还有没有别的方法获取到更多的信息呢?首先可以用 type() 函数获取变量的类型,它返回一个 Type 对象
dir() 函数获取变量的所有属性
dir()返回的属性是字符串列表,如果已知一个属性名称,要获取或者设置对象的属性,就需要用 getattr() 和 setattr( )函数了
class Person(object): def __init__(self, name, gender): self.name = name self.gender = gender class Student(Person): def __init__(self, name, gender, score): super(Student, self).__init__(name, gender) self.score = score def whoAmI(self): return 'I am a Student, my name is %s' % self.name print type(123) # <type 'int'> s = Student('Bob', 'Male', 88) print s # <class '__main__.Student'> 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'] 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'] print getattr(s, 'name') # Bob setattr(s, 'name', 'Adam') print s.name # Adam class Person(object): def __init__(self, name, gender, **kw): self.name = name self.gender = gender for k, v in kw.iteritems(): setattr(self, k, v) p = Person('Bob', 'Male', age=18, course='Python') print p.age # 18 print p.course #Python
相关文章推荐
- [Python进阶-4]类的继承/多态/多重继承/任意多参数
- Python 进阶_OOP 面向对象编程_类和继承
- python进阶六_封装与继承
- Python 进阶_OOP 面向对象编程_组合与继承
- 慕课网学习Python进阶教程,包括(函数式编程、模块、面向对象编程、类的继承、定制类)
- python进阶学习笔记(四)——python中访问限制、创建类属性、定义实例方法、定义类方法、类的继承
- 干货|python进阶系列(二)--多继承与super
- Python进阶笔记(5)_ 继承
- 廖雪峰老师——Python进阶( 类的继承 )
- Python进阶-继承中的MRO与super
- python进阶 第五章 类的继承
- python进阶——五、类的继承
- python进阶四:类的继承
- python进阶15:继承、多态和多重继承
- Python进阶-继承中的MRO与super
- [置顶] 设计模式进阶之继承、接口、抽象类
- [Python入门及进阶笔记]Python-基础-字符串小结
- python 继承
- 【转帖】使用python爬虫抓站的一些技巧总结:进阶篇
- python中的继承和抽象类的实现