python 继承练习图形的计算

app.py

app

from sector import Sector
from circle import Circle
from square import Square
while True:
		    print("几何计算器\nV9.0")
		    print("1. 正方形\n2. 圆形\n3. 扇形")
		    print("-"*30)
		    inp = input("请选择图形编号,输出q退出:")
		
		    # 正方形
		    if inp == "1":
		
		        square = Square("正方形", 0, 0)  # 初始化周长和面积为0
		        square.say_i()
		        square.sperimeter_area()
		
		    # 圆形
		    elif inp == "2":
		        circle = Circle("圆形",  0, 0)
		        circle.say_i()
		        circle.sperimeter_area()
		
		    # 扇形
		    elif inp == "3":
		        sector = Sector("扇形",  0, 0)
		        sector.say_i()
		        sector.sperimeter_area()
		
		    elif inp == "q":
		        break
		    else:
		        print("选择错误,请重新选择")

geometry.py

图形类

class Geometry():

    def __init__(self,name, perimeter, area):
        self.name = name
        self.perimeter = perimeter
        self.area = area

    # 说自己
    def say_i(self):
        print("-"*30)
        print(self.name)
        print("-" * 30)

    # 说周长说面积
    def sperimeter_area(self):
        print(f"周长是:{self.perimeter}")
        print(f"面积是:{self.area}")
        print("-" * 30)

正方形类

from  geometry import Geometry
class Square(Geometry):

    # 计算周长面积 perimeter, area
    def sperimeter_area(self):
        m = int(input("请输入边长:"))
        self.perimeter = 4 * m
        self.area = m**2
        super(Square, self).sperimeter_area()

square.py

正方形类

from  geometry import Geometry
class Square(Geometry):
    # 计算周长面积 perimeter, area
    def sperimeter_area(self):
        m = int(input("请输入边长:"))
        self.perimeter = 4 * m
        self.area = m**2
        super(Square, self).sperimeter_area()

circle.py

圆形类

from  geometry import Geometry
import math

class Circle(Geometry):

    # 计算周长面积 perimeter, area
    def sperimeter_area(self):
        r = int(input("请输入半径:"))
        self.perimeter = 2 * r * math.pi
        self.area = r ** 2 * math.pi
        super(Circle, self).sperimeter_area()

sector.py

扇形类

from  geometry import Geometry
import math
class Sector(Geometry):

    # 计算周长面积 perimeter, area
    def sperimeter_area(self):
        r = int(input("请输入边长:"))
        angle = int(input("请输入角度:"))

        self.perimeter = 2*r + angle/360*2*r*math.pi
        self.area = angle/360*math.pi*r**2
        super(Sector, self).sperimeter_area()

你可能感兴趣的:(python)