定义一个表示矩形(Juxing)的类,在类中定义矩形的两个属性:长(chang)和宽(kuan),分别用不同的构造函数实例化3个对象,并在控制台输出他们的周长和面积
时间: 2023-05-28 07:05:48 浏览: 129
class Juxing:
def __init__(self, chang, kuan):
self.chang = chang
self.kuan = kuan
def zhouchang(self):
return 2 * (self.chang + self.kuan)
def mianji(self):
return self.chang * self.kuan
jx1 = Juxing(3, 4)
jx2 = Juxing(5, 6)
jx3 = Juxing(7, 8)
print("jx1的周长为:", jx1.zhouchang(), ",面积为:", jx1.mianji())
print("jx2的周长为:", jx2.zhouchang(), ",面积为:", jx2.mianji())
print("jx3的周长为:", jx3.zhouchang(), ",面积为:", jx3.mianji())
阅读全文