作业1
作业2
class Car:
def __init__(self, motor, chassis, seat, shell):
self.motor = motor
self.chassis = chassis
self.seat = seat
self.shell = shell
def run(self):
self.motor.work()
self.seat.work()
self.chassis.work()
class Motor:
def work(self):
print("Motor is work.")
class Seat:
def work(self):
print("seat is work...")
class Chassis:
def work(self):
print("chasis is work...")
class shell:
pass
作业3
class ComputerFactory:
__obj = None
__init_flag = True
def create_car(self, brand):
if brand == '华硕':
return Huashuo()
elif brand == '联想':
return Lianxiang()
elif brand == '神州':
return Shenzhou()
else:
return "未知品牌无法创建"
def __new__(cls, *args, **kwargs):
if cls.__obj == None:
cls.__obj = object.__new__()
return cls.__obj
def __init__(self):
if ComputerFactory.__init_flag:
print("init computerfactory")
ComputerFactory.__init_flag = False
class Computer:
def calculate(self):
print("calculate computer")
class Lianxiang(Computer):
def calculate(self):
print("calculate Lianxiang")
class Huashuo(Computer):
def calculate(self):
print("calculate Huashuo")
class Shenzhou(Computer):
def calculate(self):
print("calculate Shenzhou")
作业4
class Employee:
id = 1000
def __init__(self, name, salary):
self.id = Employee.id + 1
self.name = name
self.__salary = salary
Employee.id += 1
def __add__(self, other):
if isinstance(other, Employee):
return other.salary + self.salary
@property
def salary(self):
print("salary is {0}".format(self.__salary))
@salary.setter
def salary(self, salary):
if (1000 < salary < 50000):
self.__salary = salary
else:
print("input again")
e1 = Employee("a", 3000)
e2 = Employee("b", 2000)
e3 = Employee("b", 4000)
e3.salary = 5000
print(e1.id)
print(e2.id)
print(e3.id)
print(e3.salary)