day15-作业

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

class Auto:

    def __init__(self, color, weight, speed=0, tyre=4):
        self.color = color
        self.weight = weight
        self.speed = speed
        self.tyre = tyre
        self.max_speed = 300

    def speed_up(self, value):
        """加速"""
        current_speed = self.speed + value
        if current_speed >= self.max_speed:
            return self.max_speed
        else:
            return current_speed

    def reduce_speed(self, value):
        """减速"""
        if self.speed > value:
            return self.speed - value
        else:
            return 0

    def stop(self):
        """停车"""
        self.speed = 0

class CarAuto(Auto):
    """小汽车类"""
    def __init__(self, air_con, cd, color, weight):
        super().__init__(color, weight)
        self.air_con = air_con
        self.cd = cd

    def speed_up(self, value):
        super(CarAuto, self).speed_up(value)

2.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数

class Person:
    """人类"""
    count = 0

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

p1 = Person('李白', 22)
p2 = Person('李牧', 33)
p3 = Person('李牧', 44)

print(Person.count)

3. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,要求打印这个类的对象的时候以'/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印

class Animal:
    """动物类"""
    def __init__(self, gender, color, genre, age=0):
        self.age = age
        self.gender = gender
        self.color = color
        self. genre = genre

    def __repr__(self):
        return '/%s的对象:性别-%s 年龄-%d 颜色-%s 类型-%s/' % \
               (self.__class__.__name__, self.gender, self.age, self.color, self.genre)

a1 = Animal('公', '黑色', '狗', 5)
print(a1)

你可能感兴趣的:(day15-作业)