面向对象 文件
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 rua(self):
return self.loan * self.tua()
def tua(self):
if self.time == 3:
return 0.0603
elif self.time == 5:
return 0.0612
elif self.time == 20:
return 0.0639
def fua(self):
return (self.loan + self.rua()) / (self.time * 12)
loan = int(input(“请输入贷款金额:”))
time = input(“请选择贷款年限:1.3年(36个月) 2.5年(60个月) 3.20年(240个月)”)
aa = LoanCaculator(loan, time)
print(“月供为:%f” % aa.fua())
设计Bird、fish类,都继承自Animal类,实现其方法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.bd = color
def print_info(self):
print("我是一只%s的鸟" % (self.bd))
super().print_info()
class Fish(Animal):
def __init__(self, zl):
super().__init__(2)
self.zl = zl
def print_info(self):
print("我是一只%d斤重的鱼" % (self.zl))
super().print_info()
bird = Bird(“红色”)
bird.print_info()
fish = Fish(5)
fish.print_info()
- 利用多态性,编程创建一个手机类Phones,定义打电话方法call()。创建两个子类:苹果手机类iPhone和Android手机类APhone,并在各自类中重写方法call。创建一个人类Person,定义使用手机打电话的方法use_phone_call()
class Phone():
def call(self):
print("使用功能机打电话")
class IPhone(Phone):
def call(self):
print("使用苹果手机打电话")
class APhone(Phone):
def call(self):
print("使用安卓手机打电话")
class Person():
def use_phone_call(self, phone):
phone.call()
person = Person()
person.use_phone_call(Phone())
person.use_phone_call(IPhone())
person.use_phone_call(APhone())
- 现在有一份‘邀请函.t