欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

day16-面向对象作业

程序员文章站 2022-03-07 09:24:13
定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积class Rectangular: def __init__(self, length=10, width=20): self.length = length self.width = width def get_area(self): area = self.length * self .width return area def get_pe....
  1. 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Rectangular:
    def __init__(self, length=10, width=20):
        self.length = length
        self.width = width


    def get_area(self):
        area = self.length * self .width
        return area


    def get_perimeter(self):
        perimeter = (self.length + self.width) * 2
        return perimeter

    
r1 = Rectangular()
print(f'r1的面积:{r1.get_area():.1f}')
print(f'r1的周长:{r1.get_perimeter():.1f}')
r2 = Rectangular(10, 100)
print(f'r2的面积:{r2.get_area():.1f}')
print(f'r2的周长:{r2.get_perimeter():.1f}')
  1. 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y


    def get_distence(self, point):
        distence = math.sqrt((self.x - point.x) ** 2 + (self.y - point.y) ** 2)
        return distence


p1 = Point()
p2 = Point(3, 4)
print(f'点1到点2的距离是:{p1.get_distence(p2):.1f}')
  1. 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切
PI = 3.1415926535898
class Circle:
    def __init__(self, x=0, y=0, redius=1):
        self.x = x
        self.y = y
        self.redius = redius


    def get_area(self):
        area = PI * self.redius ** 2
        return area


    def get_perimeter(self):
        perimeter = 2 * PI * self.redius
        return perimeter


    def is_circumscribed(self, point):
        center_distence = math.sqrt((self.x - point.x) ** 2 + (self.y - point.y) ** 2)
        if center_distence == self.redius + point.redius:
            return True
        else:
            return False


c1 = Circle()
c2 = Circle(2, 0, 1)
print(f'圆1的面积:{c1.get_area():.1f}')
print(f'圆1的周长:{c1.get_perimeter():.1f}')
print(f'圆2的面积:{c2.get_area():.1f}')
print(f'圆2的周长:{c2.get_perimeter():.1f}')
print(f'圆1和圆2是否相切:{c1.is_circumscribed(c2)}')
  1. 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度
class Line:
    def __init__(self, start: Point, end: Point):
        self.start = start
        self.end = end

    def get_length(self):
        length = self.start.get_distence(self.end)
        return length


p1 = Point(1, 1)
p2 = Point(2, 2)
l1 = Line(p1, p2)
print(f'线段的长度:{l1.get_length():.1f}')
  1. 定义一个狗类和一个人类:

狗拥有属性:姓名、性别和品种 拥有方法:叫唤

人类拥有属性:姓名、年龄、狗 拥有方法:遛狗

class Dog:
    def __init__(self, name=None, sex=None, var=None):
        self.name = name
        self.sex = sex
        self.var = var


    def bark(self):
        print(f'{self.name}:汪汪汪!')


class Person:
    def __init__(self,  dog: Dog, name=None, age=None):
        self.name = name
        self.age = age
        self.dog = dog


    def walk_the_dog(self):
        if self.dog:
            print(f'{self.name}遛{self.dog.name}')
        else:
            print(f'{self.name}没有狗')


d1 = Dog('馒头', '公', '比熊')
p1 = Person(d1, '小明', '22')
d1.bark()
p1.walk_the_dog()

本文地址:https://blog.csdn.net/Maple12138/article/details/114000096

相关标签: python