7-6 sdut-oop-9 计算长方形的周长和面积(类和对象) --python

2022/6/2 1:20:31

本文主要是介绍7-6 sdut-oop-9 计算长方形的周长和面积(类和对象) --python,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

知识点:
python创建多个构造方法

  1. 使用classmethod
  2. 将init的参数设为可变类型,在init语句中判断
class Rect:
    __length = 0
    __width = 0

    def __init__(self, l, w):
        self.__length = l
        self.__width = w

    @classmethod
    def initsec(self, l):
        return self(l, l)

    def leng(self):
        return self.__length

    def widt(self):
        return self.__width

    def e(self):
        return self.__width * self.__length

    def z(self):
        return 2 * (self.__width + self.__length)


while True:
    try:
        num = [int(x) for x in input().split()]
        if len(num) == 1:
            if num[0] <= 0:
                num[0] = 0
            t = Rect.initsec(num[0])

        else:
            if num[0] <= 0 or num[1] <= 0:
                num[0] = 0
                num[1] = 0
            t = Rect(num[0], num[1])

        print(t.leng(), t.widt(), t.z(), t.e())
    except:
        break


这篇关于7-6 sdut-oop-9 计算长方形的周长和面积(类和对象) --python的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程