目录
波斯猫,狸花猫以及查理兔 - 实验15 面向对象 - 《Python编程基础及应用实验教程》 - 高教社
平面矩形 - 实验15 面向对象 - 《Python编程基础及应用实验教程》 - 高教社
7-4 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承)
波斯猫,狸花猫以及查理兔 - 实验15 面向对象 - 《Python编程基础及应用实验教程》 - 高教社
class Pet:
def __init__(self,name):
self.__sName = name
def getName(self):
return self.__sName
class Rabbit(Pet):
def __init__(self,name):
Pet.__init__(self,name)
def speak(self):
print(f"Hello from rabbit {self.getName()}.")
def eat(self,iweight,iWeight):
print(f"Rabbit {self.getName()} ate {iWeight} gram's food.")
class Cat(Pet):
def __init__(self,name):
Pet.__init(self,name)
def eat(self,iWeight):
print(f"Cat {self.getName()} ate {iWeight} gram's food.")
class DragonLi(Cat):
def __init__(self,name):
Cat.__init__(self,name)
def speak(self):
print(f"Hello from dragonli cat {self.getName()}.")
class Persian(Cat):
def __init__(self,name):
Cat.__init__(self,name)
def speak(self):
print(f"Hello from persian cat {self.getName()}.")
print("-------------Rabbit Charlie--------------")
r = Rabbit("Charlie")
r.eat(100)
r.speak()
print("-------------Cat Lucy--------------")
c1 = DragonLi("Lucy")
c1.eat(200)
c1.speak()
print("-------------Cat Eddie--------------")
c2 = Persian("Eddie")
c2.eat(100)
c2.speak()
平面矩形 - 实验15 面向对象 - 《Python编程基础及应用实验教程》 - 高教社
class Rect:
def __init__(self, tl, br):
self.tl, self.br = tl, br
def width(self):
t = self.br.x - self.tl.x
return t if t > 0 else -t
def height(self):
t = self.br.y-self.tl.y
return t if t > 0 else -t
def area(self):
return self.width() * self.height()
def topRight(self):
return Point(self.br.x,self.tl.y)
def bottomLeft(self):
return Point(self.tl.x,self.br.y)
def diagonalLength(self):
return self.br -self.tl
class Point:
def __init__(self,x,y):
self.x, self.y = x, y
def __sub__(self,p):
return Point(self.x - p.x, self.y - p.y)
def __str__(self):
return str((self.x,self.y))
rt = Rect(Point(1,6),Point(7,8))
print("Vertices of rectangle rt:")
print(f"{rt.tl}-----------------------------{rt.topRight()}")
print(f"{rt.bottomLeft()}-----------------------------{rt.br}")
print("Size information of rectangle rt:")
print(f"width - {rt.width()},height - {rt.height()}")
print(f"area - {rt.area()},diagonal length - "\
f"{rt.diagonalLength():.4f}")
7-1 使用面向对象的方法求长方形的周长
class Rectangle:
def __init__(self,length,width):
self.__length = length
self.__width = width
def perimeter(self):
return 2 * (self.__length+self.__width)
def main():
A,B=map(float,input().split())
rect=Rectangle(A,B)
print(f"{rect.perimeter():.2f}")
if __name__ == "__main__":
main()
7-2 优异生查询(类和对象)
class Student(object):
def __init__(self, name, mscore, cscore, escore):
self.name = name
self.mscore = mscore
self.cscore = cscore
self.escore = escore
def getSum(self):
self.count = float(self.mscore) + float(self.cscore) + float(self.escore)
return self.count
def getBest(self):
print(self.name, self.mscore, self.cscore, self.escore)
lst = []
class_list = []
for i in range(4):
item_list = input().split()
lst.append(item_list)
for i in range(len(lst[0])):
stu_list = []
for j in range(4):
stu_list.append(lst[j][i])
stu = Student(stu_list[0], stu_list[1], stu_list[2], stu_list[3])
class_list.append(stu)
max_stu = class_list[0]
for stu1 in class_list:
if stu1.getSum() > max_stu.getSum():
max_stu = stu1
max_stu.getBest()
7-3 三维向量运算
class Vecter3:
def __init__(self,x = 0,y = 0,z = 0):
self.X=x
self.Y=y
self.Z=z
def add(self, n):
r = Vecter3()
r.X=self.X+n.X
r.Y=self.Y+n.Y
r.Z=self.Z+n.Z
return r
def __add__(self,n):
r=Vecter3()
r.X=self.X+n.X
r.Y=self.Y+n.Y
r.Z=self.Z+n.Z
return r
def __sub__(self,n):
r=Vecter3()
r.X=self.X-n.X
r.Y=self.Y-n.Y
r.Z=self.Z-n.Z
return r
def __mul__(self,n):
r=Vecter3(0)
r.X = self.X * n
r.Y = self.Y * n
r.Z = self.Z * n
return r
def __truediv__(self,n):
r=Vecter3()
r.X=self.X/n
r.Y=self.Y/n
r.Z=self.Z/n
return r
def __floordiv__(self,n):
r=Vecter3(0,0,0)
r.X=self.X//n
r.Y=self.Y//n
r.Z=self.Z//n
return r
def show(self):
print((self.X,self.Y,self.Z))
def main():
v1 = Vecter3(1,2,3)
v2 = Vecter3(3,4,5)
v3 = v1 + v2
v3.show()
v3 = v1.add(v2)
v3.show()
v4 = v1 - v2
v4.show()
v5 = v1 * 3
v5.show()
v6 = v2/2
v6.show()
v7 = v2//2
v7.show()
main()
7-4 sdut-oop-5 计算长方体和四棱锥的表面积和体积(类的继承)
import math
import abc
class Rect:
def __init__(self,l,h,z):
self.l=l
self.h=h
self.z=z
@abc.abstractmethod
def lengh(self):
return (self.l + self.h) * 2
@abc.abstractmethod
def Area(self):
return self.l * self.h
class Cubic(Rect):
def __init__(self,l,h,z):
super().__init__(l,h,z)
def V(self):
return self.l*self.z*self.h
def Area(self):
return (self.h*self.l+self.h*self.z+self.l*self.z)*2
class Pyramid(Rect):
def __init__(self,l,h,z):
super().__init__(l,h,z)
def V(self):
return 1/3*self.l*self.h*self.z
def Area(self):
a=self.l
b=self.h
c=self.z
p1=math.sqrt(a*a+b*b)
p1=p1/2
pp=math.sqrt(p1*p1+c*c)
h1=math.sqrt(pp*pp-a*a/4)
h2=math.sqrt(pp*pp-b*b/4)
s1=h1*a/2
s2=h2*b/2
s3=a*b
s4=(s1+s2)*2+s3
return s4
while True:
try:
l,h,z=map(float,input().split())
if l<=0 or h<=0 or z<=0:
print("{:.2f} {:.2f} {:.2f} {:.2f}".format(0,0,0,0))
else:
c, p = Cubic(l,h,z), Pyramid(l ,h,z)
print("{:.2f} {:.2f} {:.2f} {:.2f}".format(c.Area(), c.V(), p.Area(), p.V()))
except:
break