结果和代码 这里只涉及一个办法
方法部分
def main():
rX, rY = eval(input("Enter regular polygon x and y axis:"))
regular_num = eval(input("Enter regular number: "))
side_long = eval(input("Enter side number: "))
a = exCode07.RegularPolygon(regular_num, side_long,rX, rY)
print(f"x: {rX} y: {rY} regular polygon area is {a.getArea()} and perimeter{a.getPerimeter()}")
main()
类部分
class RegularPolygon:
def __init__(self, n, side, x, y):此处没写默认值
def __init__(self, n=3, side=1, x=0, y=0): 此处有默认值,别两个一起弄。哈哈哈
self.n = n
self.side = side
self.x = x
self.y = y
def get_x(self):
return self.x
def set_x(self, x):
self.x = x
def get_y(self):
return self.y
def set_y(self, y):
self.y = y
def get_n(self):
return self.n
def set_n(self, n):
self.n = n
def get_side(self):
return self.side
def set_side(self, y):
self.y = y
def getArea(self):
area_num1 = self.n * pow(self.side, 2)
**注意这里别用//
area_num2 = 4 * math.tan(math.pi / self.n)
area_total = area_num1 / area_num2
return area_total
def getPerimeter(self):
按书上要求的写三个对象
def main_2():
a = exCode07.RegularPolygon()
b = exCode07.RegularPolygon(6, 4)
c = exCode07.RegularPolygon(10, 4, 5.6, 7.8)
print(f"x: {a.x} y: {a.y} regular polygon area is {a.getArea()} and perimeter{a.getPerimeter()}")
print(f"x: {b.x} y: {b.y} regular polygon area is {b.getArea()} and perimeter{b.getPerimeter()}")
print(f"x: {c.x} y: {c.y} regular polygon area is {c.getArea()} and perimeter{c.getPerimeter()}")