• Python 类的继承


    1、父类、基类、超类  -----> 子类,派生类,继承类

    has关系应该使用组合而不是继承,如book类应作为student类的属性而非子类。

    总是从某个类继承,如果没有特定的类就从object类继承。

    不要忘记调用super().__init__来初始化父类,否则继承类将没有父类的属性。

    def __init__(self,args):

    super(SubClass,self).__init__(args)

    self参数已在super中传入,在__init__中将隐式传递,不需要写出(也不能写)

    实例:

    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

    2、使用isinstance(object)测试实例属性。

    print isinstance(t,Person)
    print isinstance(t,Student)
    print isinstance(t,Teacher)
    print isinstance(t,object)

    3、多态(子类和父类同名函数输出不一样)

    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
    def who_am_i(x):
        print x.whoAmI()
    
    p = Person('Tim', 'Male')
    s = Student('Bob', 'Male', 88)
    t = Teacher('Alice', 'Female', 'English')
    
    who_am_i(p)
    who_am_i(s)
    who_am_i(t)
     

    4、python多重继承

    多重继承可以组合多个类的功能。

    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...'
     
    54daf037000142d207580552
     
    A虽然被继承了两次,但它的__init__()只调用了一次。

    5、python中获取对象信息

    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
    s = Student('Bob', 'Male', 88)

    使用type()函数获取对象信息,返回一个type对象:

    type(s)

    使用dir()函数获取变量的所有属性:

    dir(s)返回一个字符串

    >>> 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']
    dir()返回的属性是字符串列表,如果已知一个属性名,要获取或设置对象的属性,就需要getattr()和setattr()函数:
    >>> getattr(s, 'name')  # 获取name属性
    'Bob'
    
    >>> setattr(s, 'name', 'Adam')  # 设置新的name属性
    
    >>> s.name
    'Adam'
    
    >>> getattr(s, 'age')  # 获取age属性,但是属性不存在,报错:
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'Student' object has no attribute 'age'
    
    >>> getattr(s, 'age', 20)  # 获取age属性,如果属性不存在,就返回默认值20:
    20
     
    在类内部使用setattr 和getattr时,第一个参数为self
  • 相关阅读:
    C# Xamarin For Android自动升级项目实战
    C# Xamarin移动开发基础进修篇
    .NET轻量级ORM框架Dapper入门精通
    ASP.NET WebApi技术从入门到实战演练
    (简单、可靠的安装方法)在Windows Server2016中安装SQL Server2016
    ASP.NET (Core) WebAPI IIS PUT和DELETE请求失败 405的解决办法
    js中判断对象是否为空的三种实现方法
    windows10如何设置只显示时间不显示日期
    NuGet微软官方中国国内镜像
    Win10找不到hosts文件解决方法
  • 原文地址:https://www.cnblogs.com/oucsheep/p/6711459.html
Copyright © 2020-2023  润新知