首页 > 后端开发 > Python教程 > 详解详细介绍l了Python类的继承

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

迷茫
发布: 2017-03-25 17:50:18
原创
1651 人浏览过

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 &#39;int&#39;>

s = Student(&#39;Bob&#39;, &#39;Male&#39;, 88)
print s  # <class &#39;main.Student&#39;>

print dir(123) # [&#39;abs&#39;, &#39;add&#39;, &#39;and&#39;, &#39;class&#39;, &#39;cmp&#39;, &#39;coerce&#39;, &#39;delattr&#39;, &#39;p&#39;, &#39;pmod&#39;, &#39;doc&#39;, &#39;float&#39;, &#39;floorp&#39;, &#39;format&#39;, &#39;getattribute&#39;, &#39;getnewargs&#39;, &#39;hash&#39;, &#39;hex&#39;, &#39;index&#39;, &#39;init&#39;, &#39;int&#39;, &#39;invert&#39;, &#39;long&#39;, &#39;lshift&#39;, &#39;mod&#39;, &#39;mul&#39;, &#39;neg&#39;, &#39;new&#39;, &#39;nonzero&#39;, &#39;oct&#39;, &#39;or&#39;, &#39;pos&#39;, &#39;pow&#39;, &#39;radd&#39;, &#39;rand&#39;, &#39;rp&#39;, &#39;rpmod&#39;, &#39;reduce&#39;, &#39;reduce_ex&#39;, &#39;repr&#39;, &#39;rfloorp&#39;, &#39;rlshift&#39;, &#39;rmod&#39;, &#39;rmul&#39;, &#39;ror&#39;, &#39;rpow&#39;, &#39;rrshift&#39;, &#39;rshift&#39;, &#39;rsub&#39;, &#39;rtruep&#39;, &#39;rxor&#39;, &#39;setattr&#39;, &#39;sizeof&#39;, &#39;str&#39;, &#39;sub&#39;, &#39;subclasshook&#39;, &#39;truep&#39;, &#39;trunc&#39;, &#39;xor&#39;, &#39;bit_length&#39;, &#39;conjugate&#39;, &#39;denominator&#39;, &#39;imag&#39;, &#39;numerator&#39;, &#39;real&#39;]

print dir(s) # [&#39;class&#39;, &#39;delattr&#39;, &#39;dict&#39;, &#39;doc&#39;, &#39;format&#39;, &#39;getattribute&#39;, &#39;hash&#39;, &#39;init&#39;, &#39;module&#39;, &#39;new&#39;, &#39;reduce&#39;, &#39;reduce_ex&#39;, &#39;repr&#39;, &#39;setattr&#39;, &#39;sizeof&#39;, &#39;str&#39;, &#39;subclasshook&#39;, &#39;weakref&#39;, &#39;gender&#39;, &#39;name&#39;, &#39;score&#39;, &#39;whoAmI&#39;]

print getattr(s, &#39;name&#39;) # Bob
setattr(s, &#39;name&#39;, &#39;Adam&#39;) 
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(&#39;Bob&#39;, &#39;Male&#39;, age=18, course=&#39;Python&#39;)
print p.age # 18
print p.course #Python
登录后复制

以上是详解详细介绍l了Python类的继承的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板