1.设计一个简单的购房商贷月供计算器类,按照以下公式计算总利息和每月还款金额:
总利息=贷款金额*利率
每月还款金额 = (贷款金额+总利息)/贷款年限
贷款年限不同利率也不同,这里规定只有如下表所示的3种年限、利率。
年限 利率
3年(36个月) 6.03%
5年(60个月) 6.12%
20年(240个月) 4.39%

class LoanCaculator():
def __init__(self, loan, time):
self.loan = loan
if time == "1":
self.time = 3
elif time == "2":
self.time = 5
elif time == "3":
self.time = 20
def get_total_interests(self):
return self.loan * self.get_interests_rate()
def get_interests_rate(self):
if self.time == 3:
return 0.0603
elif self.time == 5:
return 0.0612
elif self.time == 20:
return 0.0639
def get_monthly_payment(self):
return (self.loan +self.get_total_interests())/(self.time*12)
#注意要乘以12,加括号
loan = int(input("请输入贷款金额:"))
time = input("请选择贷款年限:1.3年(36个月) 2.5年(60个月) 3.20年(240个月)")
loan_caculate = LoanCaculator(loan, time)
print(loan_caculate.get_monthly_payment())
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()
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):
self.name = name
self.blood = blood
self.weapon = None
print("创建名为%s的玩家对象" % (self.name))
def armed(self, weapon):
self.weapon = weapon
print("%s装备了杀伤力为%d点的%s" % (self.name, weapon.lethal,
weapon.weapon_type))
def attack(self, target):
target.blood -= self.weapon.lethal
print("%s使用%s攻击%s,造成%d点伤害" % (self.name,
self.weapon.weapon_type, target.name, self.weapon.lethal))
print("%s还剩%d点血" % (target.name, target.blood))
class Weapon():
def __init__(self, weapon_type, lethal):
self.weapon_type = weapon_type
self.lethal = lethal
player = Player("大漠雄鹰",100,None)
enemy = Player("大反派",100,None)
weapon = Weapon("沙漠之鹰",50)
player.weapon=weapon
player.attack(enemy)
player.attack(enemy)
- 定义一个学生类(Student),要求:
(1)初始化数据成员数据成员(Sno,Sname,Sage);
(2)通过属性装饰器定义数据成员(Sno,Sname,Sage);
(3)定义特殊方法__str(self),该方法返回:这样的字符串“我叫张三,学号是1101,年龄为33,是一名学生”,其中张三,1101,33,根据用户的输入来显示。”;
(4)通过继承定义一个研究生类(Gra_student),并增加数据成员研究方向(research)
(5)在Gra_student类中,定义特殊方法__str(self),该方法返回:我叫李四,学号是1102,年龄为35,是一名研究生,研究方向是complex network;
(6)调用学生类s=Student(‘zs’,‘1101’,23),及类中的殊方法__str__(self),修改学生年龄为33,再次调用__str__(self);
(7)调用研究生类ga=Gra_student(‘李四’,‘1102’,35,‘complex network’),调用__str__(self)。
class student:
def __init__(self,Sname, Sno, Sage):
self.Sno = Sno
self.Sname = Sname
self.Sage = Sage
@property
def Sno(self):
return self._Sno
@Sno.setter
def Sno(self,sno):
self._Sno=sno
@property
def Sname(self):
return self._Sname
@Sname.setter
def Sname(self, sname):
self._Sname = sname
@property
def Sage(self):
return self._Sage
@Sage.setter
def Sage(self, sage):
self._Sage = sage
def __str__(self):
return '我叫%s,学号是%s,年龄是%s,是一名学生' % (str(self.Sname), str(self.Sno), str(self.Sage))
def run(self):
return '我叫%s,学号是%s,年龄是%s,是一名学生' % (str(self.Sname), str(self.Sno), str(self.Sage))
class Gra_student(student):
def __init__(self,Sname,Sno,Sage,_research):
self.Sno = Sno
self.Sname = Sname
self.Sage = Sage
self.__research =_research
@property
def _research(self,_research):
return self._research
@_research.setter
def _research(self,_resarch):
self.__research= _resarch
def __str__(self):
return '我叫%s,学号是%s,年龄是%s,是一名研究生,研究方向是%s'% (str(self.Sname), str(self.Sno), str(self.Sage),str(self.__research))
def run(self):
return '我叫%s,学号是%s,年龄是%s,是一名研究生,研究方向是%s'% (str(self.Sname), str(self.Sno), str(self.Sage),str(self.__research))
s = student('zs','1101',23)
print(s)
s.Sage=33
print(s)
yjs=Gra_student('李四','1102',35,'complex network')
print(yjs)
5.定义一个三维向量类,并定义相应的特殊方法实现两个该类对象之间的加、减运算(要 求支持运算符+、-),实现该类对象与标量的乘、除运算(要求支持运算符*、/),以及向 量长度的计算(要求使用属性实现)
注意向量的计算方法
# 5. 定义一个三维向量类,
# 并定义相应的特殊方法实现两个该类对象之间的加、减运算(要 求支持运算符+、-),
# 实现该类对象与标量的乘、除运算(要求支持运算符*、/),
# 以及向 量长度的计算(要求使用属性实现)。
class Vector3:
# 构造方法,初始化,定义向量坐标
def __init__(self,x,y,z):
self.__x = x
self.__y = y
self.__z = z
# 与一个向量相加,对应分量相加,返回新向量
def __add__(self, anotherPoint):
x = self.__x + anotherPoint.__x
y = self.__y + anotherPoint.__y
z = self.__z + anotherPoint.__z
return Vector3(x,y,z)
# 减去另一个向量,对应分量相减,返回新向量
def __sub__(self, anotherPoint):
x = self.__x - anotherPoint.__x
y = self.__y - anotherPoint.__y
z = self.__z - anotherPoint.__z
return Vector3(x, y, z)
# 向量与一个数字相乘,各分量乘以同一个数字,返回新向量
def __mul__(self, n):
x, y, z = self.__x*n, self.__y*n, self.__z*n
return Vector3(x,y,z)
# 向量除以一个数字,各分量除以同一个数字,返回新向量
def __truediv__(self, n):
x, y, z = self.__x/n, self.__y/n, self.__z/n
return Vector3(x, y, z)
# 查看向量长度,所有分量平方和的平方根
@property
def length(self):
return (self.__x**2 + self.__y**2 + self.__z**2)
def __str__(self):
return 'Vector3({},{},{})'.format(self.__x,self.__y,self.__z)
v1 = Vector3(3, 4, 5)
v2 = Vector3(5, 6, 7)
print(v1+v2)
print(v1-v2)
print(v1*3)
print(v2/2)
print(v1.length)


被折叠的 条评论
为什么被折叠?



