Python 实现 定义个矩形类,有长和宽两个实例属性,还有一个计算面积的方法

发布时间 2023-08-26 17:54:24作者: 人生努力努力努力就好
思路:

'''
1.定义矩形类
2.定义属性
属性分:类属性和实例属性
实例属性==》self.属性===》self是一个参数在一个方法中==》_init_方法

3.定义方法
def Area(self):
     s=self.length*self.width

'''
class Square:
    def __init__(self,length,width):
        # 实例属性
        self.length=length
        self.width=width
    # 方法名
    def Area(self):
        s=self.length*self.width
        # print('该面积为:%s'%s)
        return '该面积为:%s'%s # 哪里调用返回哪里

s1=Square(4,5)
a=s1.Area()
print(a)  # None==》说明没有返回值

运行截图: