面向对象二
1、封装
class Person:
def __init__(self, name, age):
self.__name = name # 私有属性
self.age = age
def __getname(self): # 私有方法
# print(self.__name)
print(f'{self.__name}')
# 实例方法
def printinfo(self):
print(f'{self.__name},{self.age}') # 打印私有属性
# return self.__getname() 将私有方法返回出去
p = Person('zhangsan', 18) # 创建的对象 实例化对象
# print(p.__name) # 报错
p.printinfo() # zhangsan,18
# print(p.__getname()) # 报错
p._Person__getname() # 张三
print(p._Person__name) # 张三
2、继承
"""单继承"""
class GrandFather(object):
def eat(self):
print('我只会吃饭!')
def drink(self):
print('我会喝!!')
def sleep(self):
print('我还会睡觉!')
class Father(GrandFather):
def eat(self):
print('我只会吃饭!')
def drink(self):
print('我会喝!!')
class Son(Father):
def eat(self):
print('我只会吃饭!')
luoxiang = Son()
luoxiang.sleep() # GrandFather
luoxiang.drink() # Father
"""多继承"""
"""多继承-继承顺序情景一 遵循左边优先的原则 """
class Father2:
def run(self):
print('father2在奔跑')
class Father1:
def run(self):
print('father1在奔跑')
class Son(Father1, Father2):
pass
ls = Son()
ls.run() # father1在奔跑
"""情景二 左边一条路走到黑 如果说此路不通的情况下 再执行后面的继承对象 """
class Grandfather:
def sleep(self):
print('爷爷睡觉!')
class Father2:
def sleep(self):
print('Father2睡觉!')
class Father1(Grandfather):
pass
class Son(Father1, Father2):
pass
ww = Son()
ww.sleep() # 爷爷睡觉!
"""情景三 左边优先 根最后执行 """
class GrandFather(object):
def sleep(self):
print('爷爷睡觉')
class Father2(GrandFather):
def sleep(self):
print('Father2睡觉!')
class Father1(GrandFather):
pass
class Son(Father1, Father2):
pass
zl = Son()
zl.sleep() # Father2睡觉!
# __mro__ 是查看我们类的继承顺序的 也是一个魔法方法
print(Son.__mro__) # 类名.方法名
# (<class '__main__.Son'>, <class '__main__.Father1'>, <class '__main__.Father2'>, <class '__main__.GrandFather'>, <class 'object'>)
"""在继承的过程中 同样私有属性 私有方法 不能被继承"""
3、方法的重写
class A():
def __init__(self):
print('A')
def test(self):
print("aaaa")
class C():
def __init__(self):
print('C')
def test(self):
print("cccc")
class B(A, C):
def __init__(self): # 当我的对象被创建成功 自动执行
print('B')
A.__init__(self) # 通过我的类名直接调用A中的init方法
super(B, self).__init__() # 重写父类的方法
super().__init__() # init方法也是能够被继承的
def test(self):
print("bbbb")
super(B, self).test() # 重写父类A的方法
super(A, self).test() # 重写父类C的方法
C.test(self) # 重写父类C的方法
b = B()
b.test()
# B
# A
# A
# A
# bbbb
# aaaa
# cccc
# cccc
4、多态
"""
根据我们传入对象的不同 得到不同的结果
"""
class Animal(object):
"""动物类"""
def func(self):
print('动物发出了声音')
class Cat(Animal):
"""猫类"""
def func(self):
print('喵 喵 喵')
class Dog(Animal):
"""狗类"""
def func(self):
print('汪 汪 汪 ')
class Hero:
def func(self):
print('这个是英雄类的方法,不是动物类的对象')
# 定义了一个函数
def work01(Animal): # 形参
Animal.func() # Dog().func()这样的形式
work01(Cat()) # 传入的对象 Cat().func()
# 喵 喵 喵
"""鸭子模型"""
class Duck:
def quack(self):
print("嘎嘎嘎嘎。。。。。")
class Bird:
def quack(self):
print("bird imitate duck....")
class goose:
def quack(self):
print("goose imitate duck....")
def in_the_forest(duck):
duck.quack()
duck = Duck()
bird = Bird()
goose = goose()
for x in [duck, bird, goose]:
in_the_forest(x)
# 嘎嘎嘎嘎。。。。。
# bird imitate duck....
# goose imitate duck....
5、练习:
设计一个Circle类来表示园,这个类包含圆的半径以及求周长和面积的函数。
再使用这个类创建半径为1~100的圆,并计算出相应的周长及面积。
from math import pi
class Circle:
def __init__(self, r):
self.r = r
def circle_circumference(self): # 圆的周长
print(f"圆半径为{self.r}的周长是:{self.r * 2 * pi:.2f}", end=" ")
def circle_area(self): # 圆的面积
print(f"面积是:{self.r ** 2 * pi:.2f}")
for i in range(1, 101):
c = Circle(i)
c.circle_circumference()
c.circle_area()
运行结果: