class Shape:
def __init__(self, x, y):
self.x = x
self.y = y
class Rectangle(Shape):
def __init__(self, x, y, w, h):
super().__init__(x, y)
self.width = w
self.height = h
def isContain(self, x, y):
if (self.x + self.width / 2 >= x and self.y + self.height / 2 >= y) or \
self.x - self.width / 2 <= x and self.y - self.height / 2 <= y:
return True
else:
return False
class Circle(Shape):
def __init__(self, x, y, r):
super().__init__(x, y)
self.r = r
def isContain(self, x, y):
if (x - self.x) ** 2 + (y - self.y) ** 2 <= self.r ** 2:
return True
else:
return False
该代码定义了两个形状类——Rectangle和Circle,它们继承自基础的Shape类。Rectangle类包含构造函数初始化矩形的坐标和宽高,并提供了检查点是否在矩形内的方法isContain。Circle类同样初始化圆心坐标和半径,并实现了判断点是否在圆内的方法isContain。
1612

被折叠的 条评论
为什么被折叠?



