老师帮我看看错在哪里了
来源:2-6 编程练习
Dawn_2020
2020-05-02 17:40:13
class Point(object):
# 自定义Point类的构造(初始化)方法
def __init__(self, x, y):
self.x=x
self.y=y
# 自定义Point类对象的格式化输出函数(string())
def string(self):
print("{X:{0},Y:{1}}".format(self.x,self.y))
class Circle(Point):
# 自定义Circle类的构造(初始化)方法
def __init__(self,x, y,radius):
super().__init__(x, y)#两个独立的父类是无法用super的
#此处写super(Circle,self).__init__(x, y)也可
self.radius=radius
def string(self):
print("该图形初始化点为:{X:{0}, Y:{1}};{半径为:{2}}".format(self.x,self.y,self.radius))
# 自定义Circle类对象的格式化输出函数(string())
class Size(object):
# 自定义Size类的构造(初始化)方法
def __init__(self, Width, Height):
self.Width = Width
self.Heigt = Height
# 自定义Size类对象的格式化输出函数(string())
def string(self):
print("{Width:{0},Height:{1}}".format(self.Width,self.Height))
class Rectangle(Point, Size):
# 自定义Rectangle类的构造(初始化)方法,并在方法中调用父类的初始化方法以完成初始化
def __init__(self, x,y,Width, Height):
Point.__init__(self,x, y)
Size.__init__(self,Width, Height)
# 自定义Rectangle类对象的格式化输出函数(string())
def Rectangle(self):
print("该图形初始化点为:{X:{0}, Y:{1}}; "
"长宽分别为:{Width:{2}, Height:{3}}".format(self.x,self.y,self.Width,self.Heigt))
if __name__ == "__main__":
# 实例化Circle对象,圆心为(5,5),半径为8
c=Circle(5,5,8)
c.string()
# 实例化Rectangle对象,顶点位置(15,15),长和宽分别为15和15
r1=Rectangle(15,15,15,15)
r1.string()
# 实例化Rectangle对象,顶点位置(40,30),长和宽分别为11和14
r2=Rectangle(40,30,11,14)
r2.string()
File "E:/Professional Software/Project/lei/hello1", line 46, in <module>
c.string()
File "E:/Professional Software/Project/lei/hello1", line 19, in string
print("该图形初始化点为:{X:{0}, Y:{1}};{半径为:{2}}".format(self.x,self.y,self.radius))
KeyError: 'X'
1回答
好帮手慕元宝
2020-05-05
同学你好:
如果解答了同学的疑问,望采纳!祝学习愉快~~~~~
相似问题