美文网首页
python_day14_homework

python_day14_homework

作者: Z_JoonGi | 来源:发表于2019-03-21 20:58 被阅读0次

1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性

# 并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。
# 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,
# 并且重新实现方法覆盖加速、减速的方法

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

    def __str__(self):
        return str(self.__dict__)[1:-1]

    @staticmethod
    def accelerate():
        """说明:汽车加速"""
        return  '加速'

    @staticmethod
    def decelerate():
        """说明:汽车减速"""
        return '减速'

    @staticmethod
    def stop():
        """说明:汽车停速"""
        return '停车'


class CarAuto(Auto):
    def  __init__(self, color, weight):
        super().__init__(color, weight)
        self.cd = '可触摸屏'
        self.air_conditioning ='超级空调'

    @staticmethod
    def accelerate():
        """说明:汽车加速"""
        return '小汽车加速'

    @staticmethod
    def decelerate():
        """说明:汽车减速"""
        return '小汽车减速'


# 创建类Auto汽车
print('==========Auto========')
A1 = Auto('黑色', '1T' )
print(A1.__str__())
print(A1.decelerate())
print(A1.accelerate())
print(A1.stop())
# 创建类CarAuto小汽车
print('==========CarAuto========')
C1 = CarAuto('白色', '2T')
print(C1.__str__())
print(C1.decelerate())
print(C1.accelerate())
print(C1.stop())


# 2.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
    __object_num = 0

    def __init__(self):
        self.name = '张'
        self.age = 0
        self.gender = '男'
        Person.__object_num += 1

    def __del__(self):
        Person.__object_num -= 1

    @classmethod
    def show_object_num(cls):
        return cls.__object_num

if __name__ == '__main__':
    p1 = Person()
    p2 = Person()
    p3 = Person()
    p4 = Person()

    print(Person.show_object_num())
    del p1
    print(Person.show_object_num())
    del p2
    print(Person.show_object_num())
    del p3
    print(Person.show_object_num())



# 3.创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,
# 要求打印这个类的对象的时候以
# '/XXX的对象: 性别-? 年龄-? 颜色-? 类型-?/' 的形式来打印
class AgeError(Exception):
    def __str__(self):
        return '年龄请输入数字'


class Animals:
    def __init__(self, gender, age, color, type):
        self.gender = gender
        self._age = age
        self.color = color
        self.type = type

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, age):
        if not isinstance(age, int):
            raise AgeError

    def __str__(self):
        return '%s的对象:性别-%s 年龄-%d 颜色-%s 类型-%s'%\
               ('Animals', self.gender, self.age, self.color, self.type)


dog = Animals('公', 1, '黄色', '狗')
print(dog.__str__())
# dog.age = '1'  #__main__.AgeError: 年龄请输入数字


# 4.写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。
# 但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
from math import pi

class ChangeAttributeError(Exception):
    def __str__(self):
        return '改属性不能赋值'


class Circle:
    def __init__(self, radius):
        self.radius = radius
        self.area = 0
        self.perimeter = 0

    def __str__(self):
        return str(self.__dict__)[1:-1]

    def area1(self):
        self.area = self.radius**2*pi
        raise ChangeAttributeError

    def perimeter1(self):
        self.perimeter = self.radius*2*pi
        raise ChangeAttributeError


c1 = Circle(1)
print(c1.__str__())
# c1.perimeter1()     #__main__.ChangeAttributeError: 改属性不能赋值
# c1.area1()             #__main__.ChangeAttributeError: 改属性不能赋值

==========Auto========
'tire': 4, 'color': '黑色', 'weight': '1T', 'speed': 50
减速
加速
停车
==========CarAuto========
'tire': 4, 'color': '白色', 'weight': '2T', 'speed': 50, 'cd': '可触摸屏', 'air_conditioning': '超级空调'
小汽车减速
小汽车加速
停车
4
3
2
1
Animals的对象:性别-公 年龄-1 颜色-黄色 类型-狗
'radius': 1, 'area': 0, 'perimeter': 0

相关文章

  • python_day14_homework

    1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性 ==========Auto======...

网友评论

      本文标题:python_day14_homework

      本文链接:https://www.haomeiwen.com/subject/wfdhvqtx.html