python面向对象之继承/多态/封装

老师说,按继承/多态/封装这个顺序来讲。

子类使用父类的方法:

#!/usr/bin/env python
# coding:utf-8

class Vehicle:
    def __init__(self,name,speed,load,power):
        self.name = name
        self.speed = speed
        self.load = load
        self.power = power

    def run(self):
        print("开动啦。")


class Benz(Vehicle):
    def __init__(self,name,speed,load,power,color):
        # Vehicle.__init__(self,name,speed,load,power) # 使用指定类名方式调用了父类方法
        # super(__class__.self).__init__(name,speed,load,power) # 使用super(__class__.self)方式
        super().__init__(name,speed,load,power) # 采用super()不传参数的方法
        self.color=color

    def show_info(self):
        print(self.name,self.speed,self.load,self.power,self.color)

    def run(self):
        # Vehicle.run(self) # 使用指定类名称的方式,调用了父类方法
        super().run()
        print("%s %s 出发啦." % (self.color,self.name))


car1 = Benz("GLK 300","120","5人","2.4T","black")
car1.show_info()
car1.run()

继承的例子:

#!/usr/bin/env python
# coding:utf-8

'''
组合与继承都是有效地利用已有类的资源的重要方式。但是二者的概念和使用场景皆不同,

1.继承的方式

通过继承建立了派生类与基类之间的关系,它是一种'是'的关系,比如白马是马,人是动物。

当类之间有很多相同的功能,提取这些共同的功能做成基类,用继承比较好,比如老师是人,学生是人

2.组合的方式

用组合的方式建立了类与组合的类之间的关系,它是一种‘有’的关系,比如教授有生日,教授教python和linux课程,教授有学生s1、s2、s3...

当类之间有显著不同,并且较小的类是较大的类所需要的组件时,用组合比较好
'''

class Dad:
    '这个是爸爸类'
    money=10
    def __init__(self,name):
        print('父类')
        self.name=name
    def hit_son(self):
        print('%s 正在打儿子' %self.name)


# class Son(Dad):
#     pass
#
# # print(Son.money)
# # Son.hit_son()  # 继承了父类的数据属性和函数属性
# s1=Son('alex')
# s1.hit_son()
# print(s1.name)
# print(Dad.__dict__)
# print(Son.__dict__)


class Son(Dad):
    money = 1000000009
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def hit_son(self):
        print('来自子类')

s1 = Son('Jerry',12)
print(s1.money)
print(Dad.money) # 子类在自己里面定义了与父类同名的属性,并没有覆盖父类的属性
s1.hit_son()

print(s1.__dict__)

继承顺序:

#!/usr/bin/env python
# coding:utf-8

## 继承的顺序

class A:
    def test(self):
        print("A")

class B(A): # 单继承
    # def test(self):
    #     print("B")
    pass

class C(A):
    def test(self):
        print("C")
    pass

class D(B):
    # def test(self):
    #     print("D")
    pass

class E(C):
    # def test(self):
    #     print("E")
    pass

class F(D,E): # 多继承
    # def test(self):
    #     print("F")
    pass

f1 = F()
f1.test()
# 新式类查找顺序 F >> D >> B >> E >> C >> A
# 经典类查找顺序 F >> D >> B >> A >> E >> C

print(F.__mro__) # python2中经典类没有 __mro__

## 如果继承关系为菱形结构,那么属性的查找方式有两种,分别是:深度优先和广度优先
## 经典类时,要查找的属性不存在时,多继承会按照深度优先的方式查找
## 新式类时,要查找的属性不存在时,多继承会按照广度优先的方式查找,最后找到object

多态的例子:

#!/usr/bin/env python
# coding:utf-8

## 多态
## python本身就是多态的.
# 由不同的类实例化得到的对象,调用同一个方法,执行的逻辑不同
# 指出了对象如何通过他们共同的属性和动作来操作及访问,而不需要考虑它们具体的类。
# 表明了动态绑定的存在,允许重载及运行时类型确定和验证。
# 体现在调用不同对象的共同方法时,才能展现出来。

class H2O:
    def __init__(self,name,temperature):
        self.name=name
        self.temperature=temperature
    def turn_ice(self):
        if self.temperature < 0:
            print('[%s]温度太低结冰了' %self.name)
        elif self.temperature > 0 and self.temperature < 100:
            print('[%s]液化成水' %self.name)
        elif self.temperature > 100:
            print('[%s]温度太高变成了水蒸气' %self.name)

class Water(H2O):
    pass
class Ice(H2O):
    pass
class Steam(H2O):
    pass

w1=Water('',25)
i1=Ice('',-20)
s1=Steam('蒸汽',3000)

# 下面三个对象调用了相同的方法,但是得到的结果却不一样。
w1.turn_ice()
i1.turn_ice()
s1.turn_ice()

猜你喜欢

转载自www.cnblogs.com/frx9527/p/python_oop3attr.html