python实验六实验六
面向对象设计还得学习面向对象设计还得学习
1.设计一个简单的购房商贷月供计算器类,按照以下公式计算总利息和每月还款金额:
总利息=贷款金额*利率
每月还款金额 = (贷款金额+总利息)/贷款年限
贷款年限不同利率也不同,这里规定只有如下表所示的3种年限、利率。
年限:3年,5年,20年
利率:6.03%,6.12%,6.39%
class Calculator():
def __init__(self,money,time):
self.money = money
if time == '1':
self.time = 3
elif time == '2':
self.time = 5
elif time == '3':
self.time = 20
def loan_total_money(self):
return (self.money)*(self.loan_rate_money())
def loan_rate_money(self):
if self.time ==3:
return 0.0603
elif self.time == 5:
return 0.0612
elif self.time == 20:
return 0.0639
def every_repayment_money(self):
return (self.money + self.loan_total_money())/(self.time*12)
money = int(input("请输入贷款金额: "))
time = input("请选择贷款年限:1.3年(36个月) 2.5年(60个月) 3.20年(240个月)")
month_money = Calculator(money,time)
print("月供为%f:"%(month_money.every_repayment_money()))
2、设计Bird、fish类,都继承自Animal类,实现其方法print_info(),输出如下信息。Animal类中的print_info()输出年龄:“我今
年XX岁了”, Bird、fish类继承和覆盖Animal类中的print_info()方法输出:“我是一只XXX, 我今年XX岁了”。
class Animal():
def __init__(self,age):
self.age = age
def print_info(self):
print("我今年%d岁了!"%(self.age))
class Bird(Animal):
def __init__(self,color):
super().__init__(4)
self.color = color
def print_info(self):
print("我是一只%s的鸟!"%(self.color))
super().print_info()
class Fish(Animal):
def __init__(self,weight):
super().__init__(2)
self.weight = weight
def print_info(self):
print("我是一只%s的鱼"%(self.weight))
super().print_info()
bird = Bird("红色")
bird.print_info()
fish = Fish("5")
fish.print_info()
3、绝地求生游戏
(1)定义玩家类Player
变量:name、blood(血量)、weapon(武器)
方法:装备武器、攻击
(2)定义武器类weapon
变量:weapon_type(武器类型)、lethal(杀伤力)
(3)攻击敌人
创建玩家和敌人对象并初始化属性
创建武器对象之后让玩家对象装备武器
玩家调用攻击方法攻击
class Player():
def __init__(self,name,blood,weapon):
评论0
最新资源