栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python

1. 定义发动机类 Motor、底盘类 Chassis、座椅类 Seat 车辆外壳类 Shell 并使用组合
关系定义汽车类。其他要求如下
定义汽车的 run()方法 里面需要调用 Motor 类的 work()方法 也需要调用座椅
类 Seat 的 work()方法 也需要调用底盘类 Chassis 的 work()方法。

 
定义发动机类Motor、底盘类Chassis、座椅类Seat 车辆外壳类Shell 并使用组合关系定义汽车类。其他要求如下 
定义汽车的run()方法 里面需要调用Motor类的work()方法 也需要调用座椅类Seat的work()方法 也需要调用底盘类Chassis的work()方法
class Motor: #定义发动机类Motor、底盘类Chassis、座椅类Seat 车辆外壳类Shell 并使用组合关系定义汽车类
 def motor_work(self):
 print( Motor Complete! )
class Chassis: #底盘类Chassis
 def chassis_work(self):
 print( Chassis Complete! )
class Seat: #座椅类Seat
 def seat_work(self):
 print( Seat Complete! )
class Shell: #车辆外壳类Shell
 pass
class Car: #使用组合关系定义汽车类
 def __init__(self):
 self.motor Motor()
 self.chassis Chassis()
 self.seat Seat()
 self.shell Shell()
 def run(self): #定义汽车的run()方法 里面需要调用Motor类的work()方法 也需要调用座椅类Seat的work()方法 也需要调用底盘类Chassis的work()方法
 self.motor.motor_work()
 self.chassis.chassis_work()
 self.seat.seat_work()
Car().run()

结果如下

以上程序通过调用Car类的实例方法run() 实现创建四个实例子对象 并调用其中的方法

2. 使用工厂模式、单例模式实现如下需求
(1) 电脑工厂类 ComputerFactory 用于生产电脑 Computer。工厂类使用单例模式
也就是说只能有一个工厂对象。
(2) 工厂类中可以生产各种品牌的电脑 联想、华硕、神舟
(3) 各种品牌的电脑使用继承实现
(4) 父类是 Computer 类 定义了 calculate 方法
(5) 各品牌电脑类需要重写父类的 calculate 方法

 
使用工厂模式、单例模式实现如下需求 
(1)电脑工厂类ComputerFactory用于生产电脑Computer。工厂类使用单例模式 也就是说只能有一个工厂对象。
(2)工厂类中可以生产各种品牌的电脑 联想、华硕、神舟
(3)各种品牌的电脑使用继承实现 
(4)父类是Computer类 定义了calculate方法
(5)各品牌电脑类需要重写父类的calculate方法
class Computer: #(4)父类是Computer类 定义了calculate方法
 def calculate(self):
 print( 父类 )
class Lenovo(Computer): #(3)各种品牌的电脑使用继承实现 
 def calculate(self):
 print( 联想function initiating··· )
class Asus(Computer):
 def calculate(self): #(5)各品牌电脑类需要重写父类的calculate方法
 print( 华硕function initiating··· )
class Shenzhou(Computer):
 def calculate(self):
 print( 神舟function initiating··· )
def computer_calculate(m):
 if isinstance(m,Computer) True:
 m.calculate()
class ComputerFactory: #(1)电脑工厂类ComputerFactory用于生产电脑Computer。工厂类使用单例模式 也就是说只能有一个工厂对象。
 __obj None
 __init_flag True
 def __new__(cls, *args, **kwargs):
 if cls.__obj None:
 cls.__obj object.__new__(cls)
 return cls.__obj
 def creat_computer(self,brand): #(2)工厂类中可以生产各种品牌的电脑 联想、华硕、神舟
 if brand 联想 :
 return computer_calculate(Lenovo()) #创建对象并实现多态调用方法
 elif brand 华硕 :
 return computer_calculate(Asus())
 elif brand 神舟 :
 return computer_calculate(Shenzhou())
 else:
 print( 异常 )
#测试工厂单例模式
c1 ComputerFactory()
c2 ComputerFactory()
print(c1,c2)
b input( 请输入电脑品牌(联想、华硕、神舟) )
ComputerFactory().creat_computer(b)

结果如下

 可见 首先实现了工厂的单例属性 其次可以通过输入不同品牌的电脑自动创建该类型电脑的实例对象 并调用对象内部的实例方法。

3. 定义一个 Employee 雇员类 要求如下
(1) 属性有 id、name、salary
(2) 运算符重载 实现两个对象相加时 默认返回他们的薪水和
(3) 构造方法要求 输入 name、salary 不输入 id。id 采用自增的方式 从 1000 开
始自增 第一个新增对象是 1001 第二个新增对象是 1002。
(4) 根据 salary 属性 使用 property 设置属性的 get 和 set 方法。set 方法要求输
入 1000-50000 范围的数字。

 
定义一个Employee雇员类 要求如下 
(1)属性有 id、name、salary
(2)运算符重载 实现两个对象相加时 默认返回他们的薪水和
(3)构造方法要求 输入name、salary 不输入id。id采用自增的方式 从1000开始自增 第一个新增对象是1001 第二个新增对象是1002。
(4)根据salary属性 使用 property设置属性的get和set方法。set方法要求输入 1000-50000范围的数字。
class Employee:
 idc 1000
 def __init__(self,name,salary): #(3)构造方法要求 输入name、salary 不输入id。id采用自增的方式
 self.__name name
 self.__salary salary
 Employee.idc 1
 self.id Employee.idc
 def __add__(self, other): #(2)运算符重载 实现两个对象相加时 默认返回他们的薪水和
 if isinstance(other,Employee) True:
 return (self.__salary other.__salary)
 else:
 return 非法运算! 
 property #(4)根据salary属性 使用 property设置属性的get和set方法。set方法要求输入 1000-50000范围的数字。
 def salary(self):
 return self.__salary
 salary.setter
 def salary(self,salary):
 if 1000 salary 50000:
 self.__salary salary
 else:
 print( 数字异常 )
e1 Employee( Kryser ,6000)
e2 Employee( Lily ,7200)
print( id1: ,e1.id) #从1000开始自增 第一个新增对象是1001 
print( id2: ,e2.id) #第二个新增对象是1002
print( 修改前薪资 ,e1.salary)
e1.salary 8000
print( 修改后薪资 ,e1.salary)
print( 读取名字 ,e1._Employee__name)
print( 测试 重写 ,e1 e2)

结果如下

可见已经实现id从1001开始的编码 其次可以通过 property装饰器读取和修改salary数据。并且可以正常读取输入的私有实例属性--name。最后完成了对__add__()方法的改写 实现两个对象相加返回工资和的功能。

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/267740.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号