美文网首页Python
4、面向对象高级

4、面向对象高级

作者: 代码充电宝 | 来源:发表于2019-04-29 08:07 被阅读3次
    (1)类的继承
    • 函数super(Student, self)将返回当前类继承的父类,即 Person
    • 然后调用init()方法,注意self参数已在super()中传入,在init()中将隐式传递,不需要写出(也不能写)
    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
    s = Student("zhangsan","man",76)
    print s.name
    print s.gender
    print s.score
    
    (2)类型判断
    • 函数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
    p = Person('Tim', 'Male')
    s = Student('Bob', 'Male', 88)
    t = Teacher('Alice', 'Female', 'English')
    print isinstance(p,Person)  # True
    print isinstance(p,Student) # False
    print isinstance(p,Teacher) # False
    print isinstance(p,object) # True
    print isinstance(s,Person)  # True
    print isinstance(s,Student) # True
    print isinstance(s,Teacher) # False
    print isinstance(s,object) # True
    print isinstance(t,Person)  # True
    print isinstance(t,Student) # False
    print isinstance(t,Teacher) # True
    print isinstance(t,object) # True
    
    • 查看变量类型
    >>> type(123)
    <type 'int'>
    >>> s = Student('Bob', 'Male', 88)
    >>> type(s)
    <class '__main__.Student'>
    
    • 查看变量所有属性名
    >>> dir(123)   # 整数也有很多属性...
    ['__abs__', '__add__', '__and__', '__class__', '__cmp__', ...]
    >>> 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']
    
    (3)继承的多态
    • 调用 xx.whoAmI()总是先查找它自身的定义,如果没有定义,则顺着继承链向上查找,直到在某个父类中找到为止
    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
    p = Person('Tim', 'Male')
    s = Student('Bob', 'Male', 88)
    t = Teacher('Alice', 'Female', 'English')
    print p.whoAmI() # I am a Person, my name is Tim
    print s.whoAmI() # I am a Student, my name is Bob
    print t.whoAmI() # I am a Teacher, my name is Alice
    
    (4)多重继承
    • 多重继承通过 super()调用init()方法时,A 虽然被继承了两次,但init()只调用一次:
    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…'
    >>> d = D('d')
    init A...
    init C...
    init B...
    init D...
    

    相关文章

      网友评论

        本文标题:4、面向对象高级

        本文链接:https://www.haomeiwen.com/subject/mwoxnqtx.html