python多态
# -*- Codeing = utf-8 -*-
# @Time : 2021-11-27 下午 1:43
# @Author : Wans
# @File : object09.py
# @Software : PyCharm
# class Person:
# def __init__(self, name):
# self.name = name
#
# def eat(self):
# print('---------->eat1')
#
# def eat(self, food):
# print('----------->eat:', food)
#
#
# p = Person('jack')
# p.eat('肉丸子')
import inspect
class Base:
def test(self):
print('------base')
class A(Base):
def test(self):
print('AAAAAAAAAA')
class B(Base):
def test(self):
print('BBBBBBBBBB')
class C(Base):
def test(self):
print('CCCCCCCCCC')
class D(A, B, C):
pass
# c = C()
# c.test()
# c.test1()
# c.test2()
d = D()
d.test()
print(inspect.getmro(D))
print(D.__mro__)
'''
python允许多继承
def 子类(父类1,父类2)
pas
如果父类有相同方法名称,搜索顺序是,
自己-父类1-父类2。。。。-父类中的父类
'''
# -*- Codeing = utf-8 -*-
# @Time : 2021-11-27 下午 2:17
# @Author : Wans
# @File : object10.py
# @Software : PyCharm
# 多继承的搜索顺序,经典类,新式类
class P1():
def foo(self):
print('p1====foo')
def bar(self):
print('p1------bar')
class P2():
def foo(self):
print('p2------.foo')
class C1(P1, P2):
pass
class C2(P1, P2):
def bar(self):
print('C2====bar')
class D1(C1, C2):
pass
d = D1()
d.foo()
d.bar()
print(D1.__mro__)
# -*- Codeing = utf-8 -*-
# @Time : 2021-11-27 下午 2:40
# @Author : Wans
# @File : object11.py
# @Software : PyCharm
"""
私有化:
__age
def __show(self):
pass
--->__类名__属性
私有化:封装 将属性私有化,定义公有set和get方法
def setAge(self, age):
判断
def getAge(self):
return self.__age
s.setAge(20)
s.getAge()
class Studnt:
def __init__(self,age):
self.__age=age
@property
def age(self):
return ...
@age.setter
def age(self,age):
self.__age=age
s = student()
s.age = 10
print(s.age)
继承:
has a
class Student:
def __init__(self,name,book):
pass
is a # 真正的继承
父类 子类
class Person:
pass
class Student(Person):
...
def study(self):
...
def
s = Student()
s.study()
1.__init__
2.重写方法
多继承:
class A:
pass
class B:
pass
class C(A,B):
pass
python3使用新式类:广度优先
python2使用经典类:深度优先
查看搜索顺序
C.__mor__
import inspect
priny(inspect.getmro(C))
"""
# 面向对象的特点:多态 封装 继承
class Person:
def __init__(self, name):
self.name = name
def feed_pet(self, pet): # pet既可以接受cat,也可以接受dog,还可以接收tiger
# 其他语言的多态只能传一个类的子类
# 如果传入的pet参数是Pet类型为真
# isinstance(obj,类) ----->判断obj是不是类的对象或者判断obj是不是该子类的对象
if isinstance(pet, Pet):
print('{}喜欢养宠物{},昵称是{}'.format(self.name, pet.role, pet.nickname))
else:
print('不是宠物类型。。。。')
class Pet:
role = ''
def __init__(self, nickname, age):
self.nickname = nickname
self.age = age
def show(self):
print('昵称:{},年龄:{}'.format(self.nickname, self.age))
class Cat(Pet):
role = '猫'
def catch_mouse(self):
print('抓老鼠....')
class Dog(Pet):
role = '狗'
def whach_house(self):
print('看家高手....')
class Tiger:
def eat(self):
print('太可怕了,能吃人')
# 创建对象
cat = Cat('花花', 2)
dog = Dog('大黄', 4)
person = Person('小明')
person.feed_pet(cat)
print('-------------')
person = Person('小花')
person.feed_pet(Tiger)
'''
pet 父类 cat dog 子类
pet 大类型 cat
'''
回顾:
面向对象的特点:
封装,继承,多态
封装:
私有化属性:定义公有的set和get方法
class Student
def __init__(self):
self.__age=age
@property
def age(self):
return self.__age
@age.setter 装饰器,防止下面将上面覆盖
def age(self,age):
self.__age=age
s = Student()
s.age = 10
继承:
父类中私有的,子类无法继承
is a:
父类 子类
class Student(Person):
pass
has a:
class Student:
def __init__(self,book,computer):
book是自定义类型 --- 系统类型
s = Student()
多继承:
class C(A,B):
pass
广度优先 从上到下
查看搜索顺序
C.__mro__
多态:
class Person:
def feed_pet(self,cat):
isinstance(pet,Pet)判断参数是否是哪个类
pass
# -*- Codeing = utf-8 -*-
# @Time : 2021-12-17 下午 11:58
# @Author : Wans
# @File : test.py
# @Software : PyCharm
class Person:
def __init__(self):
self.__money = 200
self.name = '匿名'
def show1(self):
print(self.name, self.__money)
class Studen(Person):
def __init__(self):
super().__init__()
super(Studen, self).__init__()
Person().__init__()# 用的少
self.__money = 100
def show(self):
print('money:', self.__money)
s = Studen()
s.show()
s.show1()
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 Wans!
评论
TwikooGitalk