class Shape(object):
def __init__(self, area):
self.area = area
print()
class Rectangle(object):
def __init__(self, width, height, perimeter):
self.perimeter = perimeter
self.width = width
self.height = height
def area(self):
self.area = self.width * self.height
print()
def perimeter(self):
perimeter(2*(self.width+self.height))
print()
创建一个名为Shape的父类,具有一个area方法。然后创建一个Rectangle子类,继承自Shape,并具有额外的width和height属性。该子类还应该具有一个perimeter方法来计算矩形的周长。
修正:
编写程序,创建基类Vehicle,其成员包括实例属性brand(品牌)和color(颜色),实例方法showInfo()用来输出实例属性brand和color的值;创建派生类Car,继承Vehicle类,新增实例属性seat(座位数),重写基类的实例方法showInfo ()输出所有实例属性的值。利用__init__()构造方法完成Vehicle和Car类的对象初始化工作,并编写测试代码
class Vehicle:
def __init__(self,brand,color):
self.brand = brand
self.color = color
def showinfo(self):
print(f"brand的属性值是{self.brand},color的属性值是{self.color}")
class Car:
def __init__(self,seat):
super().__init__("大本","pink")
self.seat = seat
def showInfo(self):
print(f"brand的属性值是{self.brand},color的属性值是{self.color},seat的属性值是{self.seat}")
if __name__ = '__main__': #判断当前的执行环境是不是模块本身,防止其他模块导入此模块式,失误执行测试
car1 = Car(4)
car1.showwInfo()