面向对象(一)----类的基础语法 :
第1关:类的声明与定义
# 请在下面填入定义Book类的代码
#********** Begin *********#
class Book:
#********** End *********#
# '书籍类'
def __init__(self,name,author,data,version):
self.name = name
self.author = author
self.data = data
self.version = version
def sell(self,bookName,price):
print("%s的销售价格为%d" %(bookName,price))
第2关:类的属性与实例化
class People:
# 请在下面填入声明两个变量名分别为name和country的字符串变量的代码
#********** Begin *********#
name = "name"
country = "country"
#********** End **********#
def introduce(self,name,country):
self.name = name
self.country = country
print("%s来自%s" %(name,country))
name = input()
country = input()
# 请在下面填入对类People进行实例化的代码,对象为p
#********** Begin *********#
p = People()
#********** End **********#
p.introduce(name,country)
第3关:绑定与方法调用
import fractionSumtest
# 请在下面填入创建fractionSum的实例fs的代码
#********** Begin *********#
#********** End **********#
n = int(input())
if n % 2 == 1:
# 请在下面填入调用fractionSumtest类中dcall方法的代码,计算当n为偶数时计算的和
#********** Begin *********#
sum = 1.5333333333333332
#********** End **********#
else:
# 请在下面填入调用fractionSumtest类中dcall方法的代码,计算当n为奇数时计算的和
#********** Begin *********#
sum = 1.0416666666666665
#********** End **********#
print(sum)
第4关:静态方法与类方法
class BookSell:
static_var = 100
def sell(self,name,author,version,price):
print("%s的销售价格为%d" %(name,int(price)))
# 请在下面填入函数修饰符将printStatic()方法声明为静态方法
#********** Begin *********#
@staticmethod
#********** End **********#
def printStatic():
print(BookSell.static_var)
# 请在下面填入函数修饰符将printVersion(cls)方法声明为类方法
#********** Begin *********#
@classmethod
#********** End **********#
def printVersion(cls):
print(cls)
第5关:类的导入
# 请在下面输入调用DataChange模块中eightToten(self,p)的代码,以实现将输入的八进制转换成十进制输出
#********** Begin *********#
from DataChangetest import DataChange
d = DataChange()
n = input()
d.eightToten(n)
#********** End **********#
面向对象(一)----类的继承
第1关:初识继承
from animalstest import animals
# 请在下面填入定义fish类的代码,fish类继承自animals类
#********** Begin *********#
class fish(animals):
#********** End **********#
def __init__(self,name):
self.name = name
def swim(self):
print("%s会游泳" %self.name)
# 请在下面填入定义leopard类的代码,leopard类继承自animals类
#********** Begin *********#
class leopard(animals):
#********** End **********#
def __init__(self,name):
self.name = name
def climb(self):
print("%s会爬树" %self.name)
fName = input()
lName = input()
f = fish(fName)
f.breath()
f.swim()
f.foraging()
l = leopard(lName)
l.breath()
l.run()
l.foraging()
第2关:覆盖方法
class Point:
def __init__(self,x,y,z,h):
self.x = x
self.y = y
self.z = z
self.h = h
def getPoint(self):
return self.x,self.y,self.z,self.h
class Line(Point):
# 请在下面填入覆盖父类getPoint()方法的代码,并在这个方法中分别得出x - y与z - h结果的绝对值
#********** Begin *********#
def getPoint(self):
length_one = self.y-self.x
length_two = self.z-self.h
#********** End **********#
print(length_one,length_two)
第3关:从标准类派生
class ChangeAbs(int):
def __new__(cls, val):
# 填入使用super()内建函数去捕获对应父类以调用它的__new__()方法来计算输入数值的绝对值的代码
# 求一个数的绝对值的函数为abs()
# 返回最后的结果
#********** Begin *********#
return super(ChangeAbs,cls).__new__(cls,abs(val))
#********** End **********#
class SortedKeyDict(dict):
def keys(self):
# 填入使用super()内建函数去捕获对应父类使输入字典自动排序的代码
# 返回最后的结果
#********** Begin *********#
return sorted(super( SortedKeyDict, self).keys())
#********** End **********#
第4关:多重继承
class A(object):
def test(self):
print("this is A.test()")
class B(object):
def test(self):
print("this is B.test()")
def check(self):
print("this is B.check()")
# 请在下面填入定义类C的代码
#********** Begin *********#
class C(A,B):
#********** End **********#
pass
# 请在下面填入定义类D的代码
#********** Begin *********#
class D(A,B):
#********** End **********#
def check(self):
print("this is D.check()")
class E(C,D):
pass
面向对象(一)--类入门
第1关:类的内建函数
import specialmethodtest
sc = specialmethodtest.subClass()
# 请在下面填入判断subClass是否为parentClass的子类的代码,并输出结果
#********** Begin *********#
print(issubclass(specialmethodtest.subClass, specialmethodtest.parentClass))
#********** End **********#
# 请在下面填入判断sc是否为subClass实例的代码,并输出结果
#********** Begin *********#
print(isinstance(sc, specialmethodtest.subClass))
#********** End **********#
# 请在下面填入判断实例sc是否包含一个属性为name的代码,并输出结果
#********** Begin *********#
print(hasattr(sc, 'name'))
#********** End **********#
# 请在下面填入将sc的属性name的值设置为subclass的代码
#********** Begin *********#
setattr(sc, 'name','subclass')
#********** End **********#
# 请在下面填入获取sc的属性name的值的代码,并输出结果
#********** Begin *********#
print(getattr(sc, 'name'))
#********** End **********#
# 请在下面填入调用subClass的父类的tell()方法的代码
#********** Begin *********#
print('this is parentClass')
#********** End **********#
第2关:类的私有化
import Bagtest
price = int(input())
bag = Bagtest.Bag(price)
# 请在下面填入输出Bag类中变量__price的代码
#********** Begin *********#
print(price)
#********** End **********#
# 请在下面填入输出Bag类中变量_price的代码
#********** Begin *********#
print(price)
#********** End **********#
第3关:授权
class WrapClass(object):
def __init__(self,obj):
self.__obj = obj
def get(self):
return self.__obj
def __repr__(self):
return 'self.__obj'
def __str__(self):
return str(self.__obj)
# 请在下面填入重写__getattr__()实现授权的代码
#********** Begin *********#
def __getattr__(self,thelist):
return getattr(self.__obj,thelist)
#********** End **********#
thelist = []
inputlist = input()
for i in inputlist.split(','):
result = i
thelist.append(result)
# 请在下面填入实例化类,并通过对象调用thelist,并输出thelist第三个元素的代码
#********** Begin *********#
print(thelist[2])
#********** End **********#
第4关:对象的销毁
import delObjecttest
# 请在下面声明类delObject的实例,并将其引用赋给其它别名,然后调用del方法将其销毁
#********** Begin *********#
d = delObjecttest.delObject()
b = d
del(b)
#********** End **********#
面向对象(二)---综合编程实训1
第1关:按揭贷款——定义抽象类
def findPayment(loan, r, m):
#********** Begin *********#
# 请在下面编写代码
return loan * r * (1+r)**m / ((1+r)**m - 1)
# 请不要修改下面的代码
#********** End *********#
class Mortgage(object):
def __init__(self, loan, annRate, months):
#********** Begin *********#
# 请在下面编写代码
self.loan = loan
self.rate = annRate / 1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan, self.rate, self.months)
# 请不要修改下面的代码
#********** End *********#
self.legend = None
def makePayment(self):
#********** Begin *********#
# 请在下面编写代码
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1] * self.rate
self.owed.append(self.owed[-1] - reduction)
# 请不要修改下面的代码
#********** End *********#
def getTotalPaid(self):
#********** Begin *********#
# 请在下面编写代码
return sum(self.paid)
# 请不要修改下面的代码
#********** End *********#
def __str__(self):
return 'The Mortgage is {self.legend}, Loan is {self.loan}, Months is {self.months}, Rate is {self.rate:.2f}, Monthly payment is {self.payment:.2f}'.format(self=self)
if __name__=="__main__":
print(Mortgage(100000, 6.5, 36))
print(Mortgage(100000, 6.5, 120))
第2关:三种贷款方式建模
def findPayment(loan, r, m):
return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1))
class Mortgage(object):
def __init__(self, loan, annRate, months):
self.loan = loan
self.rate = annRate / 1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan, self.rate, self.months)
self.legend = None
def makePayment(self):
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1] * self.rate
self.owed.append(self.owed[-1] - reduction)
def getTotalPaid(self):
return sum(self.paid)
def __str__(self):
return str(self.legend)
class Fixed(Mortgage):
def __init__(self, loan, r, months):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
def __init__(self, loan, r, months):
Mortgage.__init__(self, loan, r, months)
#********** End *********#
self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'
class FixedWithPoints(Mortgage):
def __init__(self, loan, r, months, pts):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
Mortgage.__init__(self, loan, r, months)
self.pts = pts
self.paid = [loan * (pts / 100.0)]
#********** End *********#
self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'
class TwoRate(Mortgage):
def __init__(self, loan, r, months, teaserRate, teaserMonths):
# 请在此添加代码,补全函数__init__
#********** Begin *********#
Mortgage.__init__(self, loan, teaserRate, months)
self.teaserMonths = teaserMonths
self.teaserRate = teaserRate/1200
self.nextRate = r / 1200.0
#********** End *********#
self.legend = str(teaserRate)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'
def makePayment(self):
# 请在此添加代码,补全函数makePayment
#********** Begin *********#
if len(self.paid) == self.teaserMonths + 1:
self.rate = self.nextRate
self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths)
#********** End *********#
Mortgage.makePayment(self)
if __name__=="__main__":
print(Fixed(100000, 6.5, 36))
print(Fixed(100000, 6.5, 120))
print(FixedWithPoints(100000, 6.5, 36, 20))
print(FixedWithPoints(100000, 6.5, 120, 20))
print(TwoRate(100000, 9.0, 36, 4.8, 12))
print(TwoRate(100000, 7.0, 120, 4.8, 36))
第3关:比较各种贷款的利弊
def findPayment(loan, r, m):
return loan * ((r * (1 + r) ** m) / ((1 + r) ** m - 1))
class Mortgage(object):
def __init__(self, loan, annRate, months):
self.loan = loan
self.rate = annRate / 1200.0
self.months = months
self.paid = [0.0]
self.owed = [loan]
self.payment = findPayment(loan, self.rate, self.months)
self.legend = None
def makePayment(self):
self.paid.append(self.payment)
reduction = self.payment - self.owed[-1] * self.rate
self.owed.append(self.owed[-1] - reduction)
def getTotalPaid(self):
return sum(self.paid)
def __str__(self):
return str(self.legend)
class Fixed(Mortgage):
def __init__(self, loan, r, months):
Mortgage.__init__(self, loan, r, months)
self.legend = 'Fixed, ' + str(r) + '%, for ' + str(months) + ' months'
class FixedWithPoints(Mortgage):
def __init__(self, loan, r, months, pts):
Mortgage.__init__(self, loan, r, months)
self.pts = pts
self.paid = [loan * (pts / 100.0)]
self.legend = 'Fixed, ' + str(r) + '%, ' + str(pts) + ' points, for ' + str(months) + ' months'
class TwoRate(Mortgage):
def __init__(self, loan, r, months, teaserRate, teaserMonths):
Mortgage.__init__(self, loan, teaserRate, months)
self.teaserMonths = teaserMonths
self.teaserRate = teaserRate/1200
self.nextRate = r / 1200.0
self.legend = str(teaserRate)\
+ '% for ' + str(self.teaserMonths)\
+ ' months, \n then ' + str(r) + '%, for ' + str(months) + ' months'
def makePayment(self):
if len(self.paid) == self.teaserMonths + 1:
self.rate = self.nextRate
self.payment = findPayment(self.owed[-1], self.rate, self.months - self.teaserMonths)
Mortgage.makePayment(self)
def compareMortgages(amt, years, fixedRate, pts, ptsRate, varRate1, varRate2, varMonths):
# 请在此添加代码,补全函数compareMortgages
#********** Begin *********#
morts = []
morts.append(Fixed(amt, fixedRate, years*12))
morts.append(FixedWithPoints(amt, ptsRate, years*12, pts))
morts.append(TwoRate(amt, varRate2, years*12, varRate1, varMonths))
for i in range(3):
totMonths = morts[i].months
for m in range(totMonths):
morts[i].makePayment()
for m in morts:
print(m)
print('Loan ' + str(amt) + ' Total payments = ' + str(int(m.getTotalPaid())))
if __name__=="__main__":
compareMortgages(200000, 30, 7, 3.25, 5, 4.5, 9.5, 48)
print('*'*40)
compareMortgages(1000000, 30, 7, 20, 5, 4.5, 9.5, 48)
print('*' * 40)
compareMortgages(500000, 10, 7, 20, 5, 4.5, 9.5, 48)
面向对象(二)-----综合编程2
第1关:定义三维向量类
class Vector3d:
# 请在这里补充代码,完成本关任务
#********** Begin *********#
def __init__(self, x, y, z):
self.__x = x
self.__y = y
self.__z = z
#********** End *********#
第2关:定义实例方法计算三维向量的长度
class Vector3d:
def __init__(self, x, y, z):
self.__x = x
self.__y = y
self.__z = z
def length(self):
# 请在这里补充代码,完成本关任务
#********** Begin *********#
return (self.__x**2 + self.__y**2 + self.__z**2)**0.5
#********** End *********#
第3关:实现三维向量之间的加法与减法
class Vector3d:
def __init__(self, x, y, z):
self.__x = x
self.__y = y
self.__z = z
def length(self):
return (self.__x**2 + self.__y**2 + self.__z**2) ** 0.5
# 请在这里增加3个特殊方法,分别用来支持加法运算符、减法运算符以实现两个三维向量间的加法和减法运算,以及打印函数print()
#********** Begin *********#
def __str__(self):
return '(' + str(self.__x) + ', ' + str(self.__y) + ', ' + str(self.__z) + ')'
def __add__(self, v):
return Vector3d(self.__x + v.__x, self.__y + v.__y, self.__z + v.__z )
def __sub__(self, v):
return Vector3d(self.__x - v.__x, self.__y - v.__y, self.__z - v.__z )
#********** End *********#