武林秘籍

el/2024/3/1 14:22:40

 

面向对象(一)----类的基础语法

第1关:类的声明与定义

# 请在下面填入定义Book类的代码
#********** Begin *********#
class Book:
#********** End *********## '书籍类'def __init__(self,name,author,data,version):self.name = nameself.author = authorself.data = dataself.version = versiondef 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 = nameself.country = countryprint("%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 = 100def 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 DataChanged = 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 = namedef swim(self):print("%s会游泳" %self.name)# 请在下面填入定义leopard类的代码,leopard类继承自animals类
#********** Begin *********#
class leopard(animals):
#********** End **********#def __init__(self,name):self.name = namedef 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 = xself.y = yself.z = zself.h = hdef 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.xlength_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 = objdef get(self):return self.__objdef __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 = ithelist.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 = loanself.rate = annRate / 1200.0self.months = monthsself.paid = [0.0]self.owed = [loan]self.payment = findPayment(loan, self.rate, self.months)# 请不要修改下面的代码#********** End *********#self.legend = Nonedef makePayment(self):#********** Begin *********## 请在下面编写代码self.paid.append(self.payment)reduction = self.payment - self.owed[-1] * self.rateself.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 = loanself.rate = annRate / 1200.0self.months = monthsself.paid = [0.0]self.owed = [loan]self.payment = findPayment(loan, self.rate, self.months)self.legend = Nonedef makePayment(self):self.paid.append(self.payment)reduction = self.payment - self.owed[-1] * self.rateself.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 = ptsself.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 = teaserMonthsself.teaserRate = teaserRate/1200self.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.nextRateself.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 = loanself.rate = annRate / 1200.0self.months = monthsself.paid = [0.0]self.owed = [loan]self.payment = findPayment(loan, self.rate, self.months)self.legend = Nonedef makePayment(self):self.paid.append(self.payment)reduction = self.payment - self.owed[-1] * self.rateself.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 = ptsself.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 = teaserMonthsself.teaserRate = teaserRate/1200self.nextRate = r / 1200.0self.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.nextRateself.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].monthsfor 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 = xself.__y = yself.__z = z#********** End *********#

第2关:定义实例方法计算三维向量的长度

class Vector3d:def __init__(self, x, y, z):self.__x = xself.__y = yself.__z = zdef 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 = xself.__y = yself.__z = zdef 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 *********#

 

 


http://www.ngui.cc/el/3419434.html

相关文章

C++技能树

来源于水印。

windows下安装depot_tools

搞了一天,冒火 首先你有科学上网,在git里设置了代理 1。 git clone下来源码: git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git 2。将depot_tools的目录添加到环境变量path中,尽量优先往上移动 …

带括号的四则运算表达式计算

最近面试了一家公司&#xff0c;笔试成绩一塌糊涂&#xff0c;没想到人家没否定我哈哈&#xff0c;给我两天时间让我写一个计算器出来&#xff0c;核心代码部分就在这了&#xff0c;可能还有考虑不周的情况&#xff0c;多多指教。 #include<bits/stdc.h> using namespace…

VS调试记录

导言 本人用的vs2010中文版 程序崩溃的原因&#xff1a; 堆栈溢出 1、读取未初始化的值 2、定义一个太大的局部变量 3、递归过深 数组访问越界 指针 1、访问空指针、野指针、未赋值的指针 2、释放已经被释放的内存 3、不恰定的强制类型转换 启动调试 菜单栏的调试按钮&…

Visual Assist X用法

主页菜单 open file in solution打开文件中的解决办法open Corresponding file打开相应文件list methods in current file列表方法在当前文件file symbol文件标记find references找到参考资料find references in file找到参考文件clone find references results寻找克隆结果fin…

QT多线程(三)线程互斥与同步

前言 线程之间存在着相互制约的关系&#xff1a; 互斥关系&#xff0c;如线程争夺I/O设备而导致一方必须等待一方使用结束后方可使用 同步关系&#xff0c;完成同一任务的线程之间&#xff0c;需要协调它们的工作而相互等待、交互 临界区 先看这个类&#xff1a; class Key {…

网络通信(socket)

服务端 scoket.h #ifndef SOCKET_H #define SOCKET_H#pragma execution_character_set("utf-8") #include <QtGui/QWidget> #include <QTcpServer> #include <QTcpSocket> #include<QTextCodec> #include "ui_socket.h"class Soc…

QT实现局域网通信(QUdpSocket)

界面 启动 vs2010 qt4.7.0如果提示缺少qtCore4.dll&#xff0c;需要将 .../qt4.7.0/lib/目录加入到环境变量机子必须在同一局域网下&#xff0c;使用ipconfig命令查找本机ipv4地址端口默认是8888&#xff0c;需要改的话将udp_socket->bind(8888) 这个改了 多个机子同时运行…

QT实现传输文件(tcpSocket)

界面截图 客户端 服务端 传输协议 源码 https://github.com/despicablee/transfer-file