day 15 homework

1. 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重新实现方法覆盖加速、减速的方法


class Auto:
    def __init__(self, color, weight, speed):
        self._count = 4
        self.color = color
        self.weight = weight
        self.speed = speed

    def speed_up(self):
        self.speed += 10
        return self.speed

    def speed_cut(self):
        self.speed -= 10
        return self.speed


class CarAuto(Auto):
    def __init__(self, color, weight, speed):
        super().__init__(color, weight, speed)
        self.air = '有空调'
        self.cd = '能放cd'

    def speed_up(self):
        self.speed += 15
        return self.speed

    def speed_cut(self):
        self.speed -= 15
        return self.speed


c1 = CarAuto('write', '10t', 50)
print(c1.color)
  1. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数

class Person:
    count = 0

    def __init__(self, name):
        self.name = name
        Person.count += 1


p1 = Person('hello')
p2 = Person('HELLO')
print(Person.count)
  1. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,

    要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印

class Animal:
    def __init__(self, sex, age, color, type1):
        self.sex = sex
        self.age = age
        self.color = color
        self.type1 = type1

    def print_ani(self):
        return '%s的对象 性别:%s 年龄:%s 颜色:%s 类型:%s' % (Animal.__name__, self.sex, self.age, self.color, self.type1)


a = Animal('男', '18', 'yellow', 'cat').print_ani()
print(a)
  1. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
class YouWantTodDead(Exception):
    def __str__(self):
        return '尝试修改只读属性值'


class Roundness:
    def __init__(self, r1: int):
        self._r1 = r1
        self._area = 3.14 * r1**2
        self._perimeter = 6.28 * r1

    def show(self):
        print(self._area, self._perimeter)

    @property
    def r1(self):
        return self._r1

    @r1.setter
    def r1(self, value):
        self._r1 = value
        self._area = 3.14 * self._r1**2
        self._perimeter = 6.28 * self._r1

    @property
    def area(self):
        return self._area

    @area.setter
    def area(self, value):
        print('不能修改面积')
        raise YouWantTodDead

    @property
    def perimeter(self):
        return self._area

    @perimeter.setter
    def perimeter(self, value):
        print('不能修改周长')
        raise YouWantTodDead


r2 = Roundness(5)
r2.show()
# r2.perimeter = 7
# r2.area = 15
r2.r1 = 15
r2.show()

你可能感兴趣的:(day 15 homework)