Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python 的继承 #11

Open
sfPPP opened this issue Apr 12, 2018 · 0 comments
Open

python 的继承 #11

sfPPP opened this issue Apr 12, 2018 · 0 comments

Comments

@sfPPP
Copy link
Owner

sfPPP commented Apr 12, 2018

python中什么是继承:

新类不必从头编写
新类从现有的类继承,就自动拥有了现有类的所有功能
新类只需要编写现有类缺少的新功能

  继承的好处:

复用已有代码
自动拥有了现有类的所有功能
只需要编写缺少的新功能

  继承的特点:

子类和父类是is关系

  python继承的特点:

总是从某个类继承
  1. 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

调用父类的方法:
super(object, self).sub_perform_create(serializer)
3.当继承的有多个父类而且父类有同名方法时,默认调用第一个父类的方法

4.getattr()、setattr()以及hasattr()

 if hasattr(obj,'attr')  ---判断某个类有没有什么属性
 getattr(obj,'attr')  ---获取某个类的什么属性
 setattr(obj,'attr',a) --将某个类某属性的直设为a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant