面向对象进阶_课后练习

class Car:
    def __init__(self,motor,chassis,seat,shell):
        self.motor = motor
        self.chassis =chassis
        self.seat =seat
        self.shell =shell
    def run(self):
        print(self.motor.work(),self.chassis.work(),self.seat.work(),self.shell.work())

class Motor:
    def work(self):
        return '发动机正常!'
class Chassis:
    def work(self):
        return '底盘正常!'
class Seat:
    def work(self):
        return '座椅正常!'
class Shell:
    def work(self):
        return '外壳正常!'

m = Motor()
c = Chassis()
s = Seat()
s2 =Shell()

car = Car(m,c,s,s2)
car.run()
class ComputerFactory:
    __obj = None
    __init_flag = True

    def __new__(cls, *args, **kwargs):
        if cls.__obj == None:
            cls.__obj = object.__new__(cls)
        return cls.__obj
    def __init__(self):
        if ComputerFactory.__init_flag:
            print('开始制造电脑......')
            ComputerFactory.__init_flag = False
    def create_computer(self,brand):
        if brand == '联想':
            return Lenove()
        if brand == '华硕':
            return Asus()
        if brand == '神州':
            return Shen()
        else:return Computer()


class Computer:
    def calculate(self):
        return '电脑制造'
class Lenove(Computer):
    def calculate(self):
        return '联想制造'
class Asus(Computer):
    def calculate(self):
        return '华硕制造'
class Shen(Computer):
    def calculate(self):
        return '神州制造'

factory = ComputerFactory()

c = factory.create_computer('联想')

print(c)
print(c.calculate())
class Employee:
    id = 1000

    @classmethod
    def newid(cls):
        cls.id += 1
        return cls.id

    def __init__(self,name,salary):
        self.name = name
        self.__salary = salary
        self.id = Employee.newid()


    @property
    def salary(self):
        print("ID:{0},姓名:{1}收入{2}".format(self.id,self.name,self.__salary))
        return self.__salary

    @salary.setter
    def salary(self,salary):
        if (1000<salary<50000):
            self.__salary = salary
        else:return '输入范围有误请重新输入'

    def __add__(self,other):
        if isinstance(other,Employee):
            return self.__salary + other.__salary
        else:return '不是同类无法相加'

emp1 = Employee('李狗蛋',20000)
emp2 = Employee('李狗蛋',20000)
emp3 = Employee('李狗蛋',20000)
print(emp1.salary)
print(emp2.salary)
print(emp2.salary)
print(emp2.salary)
print(emp3.salary)

print(emp1+emp2)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值