7-6 sdut-oop-9 计算长方形的周长和面积(类和对象) --python
2022/6/2 1:20:31
本文主要是介绍7-6 sdut-oop-9 计算长方形的周长和面积(类和对象) --python,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
知识点:
python创建多个构造方法
- 使用classmethod
- 将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的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-08有遇到过吗?同样的规则 Excel 中 比Python 结果大
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python