详解详细介绍l了python类的继承

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.course3. 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) #
s = student(‘bob’, ‘male’, 88)
print s #
print dir(123) # [‘abs’, ‘add’, ‘and’, ‘class’, ‘cmp’, ‘coerce’, ‘delattr’, ‘p’, ‘pmod’, ‘doc’, ‘float’, ‘floorp’, ‘format’, ‘getattribute’, ‘getnewargs’, ‘hash’, ‘hex’, ‘index’, ‘init’, ‘int’, ‘invert’, ‘long’, ‘lshift’, ‘mod’, ‘mul’, ‘neg’, ‘new’, ‘nonzero’, ‘oct’, ‘or’, ‘pos’, ‘pow’, ‘radd’, ‘rand’, ‘rp’, ‘rpmod’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘rfloorp’, ‘rlshift’, ‘rmod’, ‘rmul’, ‘ror’, ‘rpow’, ‘rrshift’, ‘rshift’, ‘rsub’, ‘rtruep’, ‘rxor’, ‘setattr’, ‘sizeof’, ‘str’, ‘sub’, ‘subclasshook’, ‘truep’, ‘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

以上就是详解详细介绍l了python类的继承的详细内容,更多请关注 第一php社区 其它相关文章!

Posted in 未分类

发表评论