第八章作业

1443-李同学

发表文章数:5

首页 » Python » 正文

1.

第八章作业
答案:
第八章作业
第八章作业
答案:
第八章作业
问题:
第八章作业
答案:
第八章作业
问题:
第八章作业
答案:

第八章作业

2

第八章作业
答案:

class Motor:
    def work(self):
        print('motor is working {}'.format(self))

class Seat:
    def work(self):
        print('seat is working {}'.format(self))

class Chassis:
    def work(self):
        print('chassis is working {}'.format(self))

class Shell:
    pass

class Car:
    def run(self,m,s,c):
        return m.work(),s.work(),c.work()

m1=Motor()
s1=Seat()
c1=Chassis()
car1=Car()
car1.run(m1,s1,c1)

————————————————————————————————————————————————

D:/anaconda3/envs/tf14/python.exe D:/PycharmProjects/MyTest/Day_1/test2.py
motor is working <__main__.Motor object at 0x0000020140731B08>
seat is working <__main__.Seat object at 0x0000020140731F08>
chassis is working <__main__.Chassis object at 0x0000020140731F88>

Process finished with exit code 0

3

第八章作业

class Computer:
    def calculate(self):
        print('Computer is calculating')

class Lenovo(Computer):
    def calculate(self):
        print('Lenovo is calculating')

class Asus(Computer):
    def calculate(self):
        print('Asus is calculating')

class Hasee(Computer):
    def calculate(self):
        print('Hasee is calculating')

class ComputerFactory:
    __obj=None
    __init_flag=True

    def __init__(self):
        if ComputerFactory.__init_flag==True:
            print('init is running')
            ComputerFactory.__init_flag=False

    def __new__(cls,*args,**kwargs):
        if cls.__obj==None:
            cls.__obj=object.__new__(cls)

        return cls.__obj

    def createCom(self,name):
        self.name=name
        if self.name=='Lenovo':
            return Lenovo()
        elif self.name=='Asus':
            return Asus()
        elif self.name=='Hasee':
            return Hasee()
        else:
            print('brand not available')

factory=ComputerFactory()
l1=factory.createCom('Lenovo')
l1.calculate()
l2=factory.createCom('Asus')
l2.calculate()
l3=factory.createCom('Hasee')
l3.calculate()

4.

第八章作业

class Employee:
    id=1000
    def __init__(self,name,salary):
        self.name=name
        self.__salary=salary
        self.id=Employee.id + 1
        Employee.id=Employee.id+1

    @property
    def salary(self):
        return self.__salary

    @salary.setter
    def salary(self,salary):
        if 1000<=salary<=50000:
            self.__salary=salary
        else:
            print('salary input is incorrect')

    def __add__(self,other):
        return "{0}--{1}".format(self.name,other.salary)


e1=Employee('li','3000')
print(e1.name)

e2=Employee('er','300')
print(e2.id)


print(e1 + e1)

print(e1.salary)
e1.salary=2000
print(e1.salary)


D:/anaconda3/envs/tf14/python.exe D:/PycharmProjects/MyTest/Day_1/test2.py
li
1002
li--3000
3000
2000

Process finished with exit code 0

未经允许不得转载:作者:1443-李同学, 转载或复制请以 超链接形式 并注明出处 拜师资源博客
原文地址:《第八章作业》 发布于2020-12-03

分享到:
赞(0) 打赏

评论 抢沙发

评论前必须登录!

  注册



长按图片转发给朋友

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏

Vieu3.3主题
专业打造轻量级个人企业风格博客主题!专注于前端开发,全站响应式布局自适应模板。

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

Q Q 登 录
微 博 登 录