标签:meta init 定义 static ati 多态 cti ssm 查找
import abc
class user(metaclass = abc.ABCMeta):
#属性
username=‘‘
#方法
#添加用户
@abc.abstractmethod
def add (self,name,pwd):
pass
#删除用户
@abc.abstractclassmethod
def.dele (cls,pp):
pass
#修改用户
@abc.abstractstaticmethod
def mod():
pass
#发现用户
def find (self):
print(‘查找‘)
class UFUser(user)
def add(self,name,pwd):
print(‘增添用户‘)
class YJUser(UFUser):
@classmethod
def dele(cls,dd):
print(‘删除用户‘)
class SYUser(YJUser):
@staticmethod
def mod():
print(‘修改用户‘)
user = SYUser
#添加操作
user.add(‘瑞鑫‘,’222‘)
#删除操作
user.dele(222)
#修改操作
user.mod
#查找操作
user。find()
-----------------------------------------------------------------------
#多态的实现
#1.设计抽象类
import abc
class Animal(metaclass = abc.ABCMeta):
@abc.abstractmethod
def niao (self):
pass
@abc.abstractmethod
def la (self):
pass
@abc.abstractmethod
def jiao(self):
pass
#定义小狗类
class Dog(Animal):
def niao(self):
print(‘小狗叫‘)
def la(self):
print(‘厕所‘)
def jiao(self)
print(‘小狗叫‘)
#定义小猫类
class Chick(Animal):
def niao(self):
print(‘小鸡叫‘)
def la(self):
print(‘嘿嘿嘿‘)
def jiao(self):
print(‘小鸡叫’)
#动物行为类
#定义初始化方法
def __init__(self,animal):
#将动物存入对象成员
self.animal = animal
def niao(self):
self.animal.niao()
def la(self):
self.animal.la()
def jiao(self):
self.animal.jiao()
#实例化小狗
xiaohei = Dog()
#实例化小猫
miaomiao = Cat()
#实例化小鸡
huahua=Chick()
#实例化行为类对象
a = Action(xiaohei)
#调用行为对象中的动物
a.niao()
a.jiao()
a.la()
class Chick(Animal):
def niao(self):
print(‘小猫叫‘)
def la(self):
print(‘哈哈哈‘)
def jiao(self):
print(‘小猫瞄’)
标签:meta init 定义 static ati 多态 cti ssm 查找
原文地址:http://www.cnblogs.com/hao-jiang-tao/p/7867738.html