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)