当前位置:Gxlcms > Python > 详解详细介绍l了Python类的继承

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

时间:2021-07-01 10:21:17 帮助过:22人阅读

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', '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类的继承的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行