面向对象作业

  1. 定义一个狗类和一个人类:

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

class Dog:

    def dog_language(self, dog_name, dog_sex, dog_various):
    	self.dog_name = dog_name
    	self.dog_sex = dog_sex
    	self.dog_various = dog_various
        print('汪汪')
    

dg = Dog()
dg.dog_language('大白', '公', '土狗')

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

class People:

    def walk_the_dog(self, name, age, pet):
    	self.name = name
    	self.age = age
    	self.pet = None
        print('遛狗')


pe = People()
pe.walk_the_dog('小明', 18, Dog('小黑', '母', '泰迪'))
  1. 定义一个矩形类,拥有属性:长、宽 拥有方法:求周长、求面积
class Rectangle:
    long = 10
    wide = 6

    def perimeter(self):
        return (Rectangle.long + Rectangle.wide) * 2

    def area(self):
        return Rectangle.long * Rectangle.wide


rt = Rectangle()
print(rt.perimeter())
print(rt.area())
  1. 定义一个二维点类,拥有属性:x坐标、y坐标 拥有方法:求当前点到另外一个点的距离
class Coordinate:
    x = 3
    y = 4

    def distance(self):
        self.x = 6
        self.y = 8
        return ((self.x - Coordinate.x) ** 2 + (self.y - Coordinate.y) ** 2) ** 0.5


cd = Coordinate()
print(cd.distance())
  1. 定义一个圆类,拥有属性:半径、圆心 拥有方法:求圆的周长和面积、判断当前圆和另一个圆是否外切
class Circle:
    pi = 3.1415926
    radius = 3
    center = (2, 0)

    def perimeter(self):
        return 2 * Circle.pi * Circle.radius

    def area(self):
        return Circle.pi * (Circle.radius ** 2)

    def just_combination(self):
        self.radius = 4
        self.center = (1, 0)

        if ((self.center[0] - Circle.center[0]) ** 2 + (self.center[1] - Circle.center[1]) ** 2) ** 0.5 == self.radius + Circle.radius:
            print('两个圆外切')
        else:
            print('两个圆非外切')


ce = Circle()
print(ce.perimeter())
print(ce.area())
ce.just_combination()
  1. 定义一个线段类,拥有属性:起点和终点, 拥有方法:获取线段的长度
class Segment:
    origin = (3, 0)
    destination = (5, 2)

    def __long__(self):
        return ((Segment.origin[0] - Segment.destination[0]) ** 2 + (Segment.origin[1] - Segment.destination[1]) ** 2) ** 0.5


sg = Segment()
print(sg.__long__())
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值