'''
1. 什么是多态
多态指的是同一种/类事物的不同形态
2. 为何要用多态
多态性:在多态的背景下,可以在不用考虑对象具体类型的前提下而直接使用对象
多态性的精髓:统一
3. 如何用多态
'''
'''
class Animal:
def speak(self):
pass
class People(Animal):
def shuo(self):
print('say hello')
class Dog(Animal):
def jiao(self):
print('汪汪汪')
class Pig(Animal):
def chang(self):
print('哼哼哼')
obj1=People()
obj2=Dog()
obj3=Pig()
# obj1.speak()
# obj2.speak()
# obj3.speak()
def speak(animal):
animal.speak()
speak(obj1)
speak(obj2)
speak(obj3)
s1='hello'
l1=[1,2,3]
t1=(1,2)
# changdu(s1)
# size(l1)
# kuangdu(t1)
print(len(s1)) #s1.__len__()
print(len(l1)) #l1.__len__()
print(len(t1)) #t1.__len__()
'''
import abc
class Animal(metaclass=abc.ABCMeta):
@abc.abstractmethod
def speak(self):
pass
@abc.abstractmethod
def run(self):
pass
# Animal() # 父类只是用来建立规范的,不能用来实例化的,更无需实现内部的方法
class People(Animal):
def speak(self):
print('say hello')
def run(self):
pass
class Dog(Animal):
def speak(self):
print('汪汪汪')
def run(self):
pass
class Pig(Animal):
def speak(self):
print('哼哼哼')
def run(self):
pass
obj1=People()
obj2=Dog()
obj3=Pig()
# python崇尚鸭子类型
class Disk:
def read(self):
print('Disk read')
def write(self):
print('Disk write')
class Memory:
def read(self):
print('Mem read')
def write(self):
print('Mem write')
class Cpu:
def read(self):
print('Cpu read')
def write(self):
print('Cpu write')
obj1=Disk()
obj2=Memory()
obj3=Cpu()
obj1.read()
obj2.read()
obj3.read()
# property装饰器是用来将类内的函数属性伪装成数据属性
# class People:
# def __init__(self,name,weight,height):
# self.name=name
# self.weight=weight
# self.height=height
#
# @property
# def bmi(self):
# return self.weight / (self.height ** 2)
#
# obj=People('egon',80,1.83)
# obj.height=1.85
# obj.weight=75
# print(obj.bmi())
# print(obj.bmi)
# 了解....
class People:
def __init__(self,name):
self.__name=name
@property
def name(self):
return '<名字:%s>' %self.__name
@name.setter
def name(self,obj):
if type(obj) is not str:
print('name必须为str类型')
return
self.__name=obj
@name.deleter
def name(self):
# print('不让删')
del self.__name
@name.getter
obj=People('egon')
# print(obj.name)
# obj.name='EGON'
# obj.name=123
# print(obj.name)
del obj.name
print(obj.__dict__)
# class People:
# def __init__(self,name):
# self.__name=name
#
# def get_name(self):
# return '<名字:%s>' %self.__name
#
# def set_name(self,obj):
# if type(obj) is not str:
# print('name必须为str类型')
# return
# self.__name=obj
#
# def del_name(self):
# # print('不让删')
# del self.__name
#
# name=property(get_name,set_name,del_name)
#
# obj=People('egon')
#
# # print(obj.name)
# # obj.name='EGON'
# # obj.name=123
# # print(obj.name)
#
# del obj.name
# print(obj.__dict__)