三大特性作业

2019-01-25  本文已影响0人  夜中光
class Person(object):
    def __init__(self):
        self.classnames=[]
        self.prices=[]
    def add(self,classname,price):
        self.classnames.append(classname)
        self.prices.append(price)
class Teacher(Person):
    def __init__(self):
        super(Teacher).__init__()
        self.classnames=[]
        self.prices = []
    def printcls(self):
        print('老师教授课程:')
        print(self.classnames,self.prices)
tea = Teacher()
tea.add('数学',90)
tea.add('应用',80)
tea.printcls()


import abc
class Pet(metaclass=abc.ABCMeta):
    def __init__(self,name):
        self.__name=name
    @abc.abstractmethod
    def eat(self):
        print('宠物在吃')
    @property
    def getname(self):
        return self.__name
    @getname.setter
    def setname(self,newname):
        self.__name=newname
class Cat(Pet):
    def __init__(self, getname,type):
      super().__init__(getname)
      self.__type=type
    @property
    def gettype(self):
        return self.__type

    @gettype.setter
    def setname(self, newtype):
        self.__name = newtype
    def eat(self):
        print('猫在吃')
cat=Cat('tom','cat')

class Pig(Pet):
    def __init__(self, getname,type):
      super().__init__(getname)
      self.__type=type
    @property
    def gettype(self):
        return self.__type

    @gettype.setter
    def setname(self, newtype):
        self.__name = newtype
    def eat(self):
        print('猪在吃')
pig=Pig('佩琪','pig')



class Dog(Pet):
    def __init__(self, getname,type):
      super().__init__(getname)
      self.__type=type
    @property
    def gettype(self):
        return self.__type

    @gettype.setter
    def setname(self, newtype):
        self.__name = newtype
    def eat(self):
        print('狗在吃')
dog=Dog('哮天犬','dog')



class Master:
    def __init__(self,name,pet):
        self.__name=name
        self.__pet=pet

    @property
    def getname(self):
        return self.__name

    @property
    def getpet(self):
        return self.__pet
    def feed(self):
        print(self.getname,'准备好食物')
        print(self.getpet,'正在进食')
        self.siyang.eat()
mas1=Master('杨戬','dog')
mas2=Master('egon','Pet')
mas3=Master('alex','Pet')

mas1.siyang=dog
mas2.siyang=pig
mas3.siyang=cat


mas1.feed()
mas2.feed()
mas3.feed()
上一篇下一篇

猜你喜欢

热点阅读