面向对象——继承

类的继承

  • 102.继承
  • 103.方法的重写
  • 104.object根类
  • 105.重写__str__()方法
  • 106.多重继承
  • 108.super获取父类的定义
  • 109.多态
  • 110.特殊方法和运算符重载
  • 特殊属性
  • 对象的浅拷贝和深拷贝

102.继承

面向对象——继承_第1张图片
面向对象——继承_第2张图片

#测试继承语法结构

class Person:
    def __init__(self,name,age):
        self.name = name
        self.__age = age  #私有属性

    def say_age(self):
        pass
        #print('年龄是{0}'.format(self.age))

class Student(Person):
    def __init__(self,name,age,score):
        Person.__init__(self,name,age)
        self.score = score

print(Student.mro())  #Student.mro()显示子类的继承结构
s = Student("马宝林",18,80)
s.say_age()
#print(s.age)  #父类里面是私有的,私有方法属性,子类继承了但是不能用
print(s._Person__age)
print(dir(s))
print(s.name)
D:\python\python37\python.exe F:/python/mypro01/mypro07.py
[<class '__main__.Student'>, <class '__main__.Person'>, <class 'object'>]
18
['_Person__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'say_age', 'score']
马宝林

103.方法的重写

面向对象——继承_第3张图片

#测试继承方法的重写

class Person:
    def __init__(self,name,age):
        self.name = name
        self.__age = age  #私有属性

    def say_age(self):
        print('我的年龄是{0}'.format(self.__age))

    def say_introduction(self):
        print("我的名字是:{}".format(self.name))

class Student(Person):
    def __init__(self,name,age,score):
        Person.__init__(self,name,age)
        self.score = score

    def say_introduction(self):
        #重写父类的方法
        print("报告老师,我的名字是:{}".format(self.name))


s = Student("马宝林",18,80)
s.say_age()
s.say_introduction()
D:\python\python37\python.exe F:/python/mypro01/mypro08.py
我的年龄是18
报告老师,我的名字是:马宝林

104.object根类

面向对象——继承_第4张图片

class A:
    pass
class B(A):
    pass
class C(B):
    pass
print(C.mro())
print(C.__mro__)
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]
(<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)

面向对象——继承_第5张图片

#object跟类

class Person:
    def __init__(self,name,age,sex):
        self.name = name
        self.age = age
        self.sex = sex
    def say_age(self):
        print("{0}的年龄为:{1}。".format(self.name,self.age))

obj = object()  #根类
print(dir(obj))

s = Person("马宝林",25,"Man")
print(dir(s))

['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'name', 'say_age', 'sex']

面向对象——继承_第6张图片面向对象——继承_第7张图片

105.重写__str__()方法

面向对象——继承_第8张图片

#重写object的__str__方法

class Person:
    def __init__(self,name):
        self.name = name

    def __str__(self):
        return "此类的名字是:{}".format(self.name)

p = Person("sxt")
print(p)

106.多重继承

面向对象——继承_第9张图片
面向对象——继承_第10张图片

#多重继承
class A:
    def aa(self):
        print("aa")

class B:
    def bb(self):
        print("bb")

class C(B,A):
    def cc(self):
        print("cc")

c = C()
c.cc()
c.bb()
c.aa()

108.super获取父类的定义

面向对象——继承_第11张图片

#测试super(),代表父类的定义,而不是对象
class A:
    def say(self):
        print("A:",self)

class B(A):
    def say(self):
        #A.say(self)
        super().say()  #与A.say()的效果一样
        print("B:",self)

c = B()
c.say()
D:\python\python37\python.exe F:/python/mypro01/mypro11.py
A: <__main__.B object at 0x000001998687D668>
B: <__main__.B object at 0x000001998687D668>

109.多态

面向对象——继承_第12张图片

#多态
#根据对象不同调用不同的方法
class Man:
    def eat(self):
        print("我饿啦!我要吃饭。")
class Chinese(Man):
    def eat(self):
        print("中国人用筷子吃饭。")
class English(Man):
    def eat(self):
        print("英国人用叉子吃饭。")
class Indian(Man):
    def eat(self):
        print("印度人用右手吃饭。")

def manEat(m):
    if isinstance(m,Man):   #Chinese是Man的子类
        m.eat()
    else:
        print("现在不可以吃饭。")

manEat(Chinese())

110.特殊方法和运算符重载

面向对象——继承_第13张图片
面向对象——继承_第14张图片
面向对象——继承_第15张图片

#运算符的重载

class Person:
    def __init__(self,name):
        self.name = name

    def __add__(self,other):
        if isinstance(other,Person):
            return "{0}--{1}".format(self.name,other.name)
        else:
            print("不是同一类不可以相加。")
    def __mul__(self, other):
        if isinstance(other,int):
            return self.name*3


p1 = Person("mabaolin")
p2 = Person("maiyulong")
x = p1+p2
print(x)
print(p1*3)

D:\python\python37\python.exe F:/python/mypro01/mypro13.py
mabaolin--maiyulong
mabaolinmabaolinmabaolin

特殊属性

面向对象——继承_第16张图片

class A:
    pass
class B(A):
    pass
class C(B):
    def __init__(self,nn):
        self.nn = nn
    pass
c = C(3)
print(dir(c))  #对象的所有属性和方法
print(c.__dict__)  #对象的属性字典
print(c.__class__)  #对象所属的类
print(C.__bases__)  #类的积累元组(多继承)
print(C.__mro__)  #类的层次结构
print(A.__subclasses__())  #子类列表
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'nn']
{'nn': 3}
<class '__main__.C'>
(<class '__main__.B'>,)
(<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>)
[<class '__main__.B'>]

对象的浅拷贝和深拷贝

面向对象——继承_第17张图片

#浅拷贝和深拷贝
import copy
class MobilPhone:
    def __init__(self,cpu,screen):
        self.cpu = cpu
        self.screen = screen
class CPU:
    def calculate(self):
        print("数字计算。")
        print("cpu对象:",self)
class Screen:
    def show(self):
        print("显示一幅好看的画。")
        print("screen对象:",self)
#测试变量赋值
'''

m1 = MobilPhone()
m2 = m1
print(m1)
print(m2)
'''
#测试浅拷贝
c1 = CPU()
s1 = Screen()
m1 = MobilPhone(c1,s1)
m2 = copy.copy(m1)

print("测试浅拷贝")
print(m1,m1.cpu,m1.screen)#cpu和screen一样,m1和m2不一样
print(m2,m2.cpu,m2.screen)

#测试深拷贝
m3 = copy.deepcopy(m1)
print("测试深拷贝")
print(m1,m1.cpu,m1.screen)#cpu和screen都不一样
print(m3,m3.cpu,m3.screen)
D:\python\python37\python.exe F:/python/mypro01/mypro14.py
测试浅拷贝
<__main__.MobilPhone object at 0x000001E153B3DEF0> <__main__.CPU object at 0x000001E1538560F0> <__main__.Screen object at 0x000001E153B3DE10>
<__main__.MobilPhone object at 0x000001E153B52198> <__main__.CPU object at 0x000001E1538560F0> <__main__.Screen object at 0x000001E153B3DE10>
测试深拷贝
<__main__.MobilPhone object at 0x000001E153B3DEF0> <__main__.CPU object at 0x000001E1538560F0> <__main__.Screen object at 0x000001E153B3DE10>
<__main__.MobilPhone object at 0x000001E153B625F8> <__main__.CPU object at 0x000001E153B625C0> <__main__.Screen object at 0x000001E153B626D8>

你可能感兴趣的:(python,类)