要求
1. 创建一个类叫做Shape,他有两个实例属性x,y表示中心坐标
2. 创建一个类叫做Rect 继承Shape 并新增两个属性w,h表示宽高
3. 给Rect创建一个函数def is_in(x1,y1) 判断x1,y1是否矩形内
代码
class Shape:
def __init__(self, x, y):
self.x = x
self.y = y
def show_info(self):
return self.x, self.y
class Rect(Shape):
def __init__(self, width, height, x, y):
super().__init__(x, y)
self.width = width
self.height = height
def is_in(self):
if (self.x, self.y) <= (self.width/2, self.height/2) and (self.x, self.y) <= (self.height/2, self.width/2):
print("坐标在矩形中")
else:
print("坐标不在在矩形中")
r = Rect(8,4,7,9)
r.is_in()