day15-面向对象作业2

  1. 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积

    class Rectangle:
        def __init__(self, length, width):
            self.length = length
            self.width = width
    
        def girth(self):
            return (self.length + self.width) * 2
    
        def area(self):
            return self.width * self.length
    
    
    rectangle = Rectangle(20, 10)
    print(rectangle.girth())
    print(rectangle.area())
    
  2. 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离

    class Point:
        def __init__(self, point_x, point_y):
            self.point_x = point_x
            self.point_y = point_y
    
        def distance(self, point):
            return ((self.point_x - point.point_x) ** 2 + (self.point_y - point.point_y) ** 2) ** 0.5
    
    
    point1 = Point(0, 0)
    point2 = Point(3, 4)
    result = point1.distance(point2)
    print(result)
    
  3. 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切

    class Point:
        def __init__(self, point_x, point_y):
            self.point_x = point_x
            self.point_y = point_y
    
        def distance(self, point):
            return ((self.point_x - point.point_x) ** 2 + (self.point_y - point.point_y) ** 2) ** 0.5
          
          
    class Circle(Point):
        pai = 3.14159
    
        def __init__(self, point_x, point_y, r):
            super().__init__(point_x, point_y)
            self.r = r
    
        def girth(self):
            return self.r * self.pai * 2
    
        def area(self):
            return self.pai * self.r ** 2
    
        def circumscribed(self, circle):
            if self.distance(circle) == self.r + circle.r:
                return '外切'
            else:
                return '不外切'
    
    
    
    circle1 = Circle(0, 0, 3)
    circle2 = Circle(3, 4, 2)
    print(circle1.girth)
     print(circle2.area)
    print(circle1.circumscribed(circle2))
    
  4. 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度

    class Line():
        def __init__(self, start: tuple, end: tuple):
            self.start = start
            self.end = end
    
        def length(self):
            return ((self.start[0] - self.end[0]) ** 2 + (self.start[1] - self.end[1]) ** 2) ** 0.5
    
    
    line = Line((0, 0), (3, 4))
    print(line.length())
    

你可能感兴趣的:(python,开发语言)