day14-homework

from math import pi
# 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性,
# 并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。


class AutoError(Exception):
    def __str__(self):
        return '汽车重量在1-10吨'


class Auto:
    """汽车"""

    def __init__(self, model, color, weight=1.8, tire=4, max_speed=200):
        self.tire = tire
        self.model = model
        self.color = color
        self.weight = weight
        self._max_speed = max_speed

    @property
    def max_speed(self):
        return str(self._max_speed)+"km/h"

    @max_speed.setter
    def max_speed(self, x):
        if not isinstance(x, float):
            raise TabError
        elif not 1.0 <= x <= 10.0:
            raise AutoError
        else:
            self._max_speed = x

    # 加速 speed up
    def speed_up(self):
        print(self.model+'加速')

    # 减速 speed cut
    def speed_cut(self):
        print(self.model+'减速')

    # 停车 stop
    def stop(self):
        print(self.model+'停车')


# 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD属性,并且重
# 新实现方法覆盖加速、减速的方法
class CarAuto(Auto):
    """小汽车"""

    def __init__(self, model, cd, color, conditioner='海尔', weight=1.8, tire=4, max_speed=200):
        super().__init__(color, weight, tire, max_speed, model)
        self.cd = cd
        self.conditioner = conditioner

    @staticmethod
    def speed_up():
        print('踩油门加速')

    @staticmethod
    def speed_cut():
        print('踩刹车')


# 创建一个Person类,添加一个类字段用来统计Person类的对象的个数
class Person:
    """人类"""
    number = 0

    def __init__(self, name,age=18, gender='女人'):
        self.name = name
        self.eag = age
        self.gender = gender
        # self.persons = []
        Person.number += 1


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


class Animal:
    """动物"""

    def __init__(self, age=0, types='猫科', color='黄色', gender='雌'):
        self.gender = gender
        self.age = age
        self.color = color
        self.types = types

    def __repr__(self):
        return '/'+str(self.__dict__)[1:-1]+'/'


mao = Animal()
print(mao)


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


class CircleError(Exception):
    """圆报错"""
    def __str__(self):
        return '不能给周长和面积赋值'


class Circle:
    """圆"""

    def __init__(self, r=1, s=None, long=None):
        self.r = r
        if s or long:
            raise CircleError
        self.s = pi*r**2
        self.long = 2*pi*r


o1 = Circle(2)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性 ==========Auto======...
    Z_JoonGi阅读 703评论 0 0
  • 1.声明⼀个电脑类: 属性:品牌、颜⾊、内存⼤⼩⽅法:打游戏、写代码、看视频a.创建电脑类的对象,然后通过对象点的...
    Voyaserfuerte阅读 95评论 0 0
  • 1.声明一个电脑类:属性:品牌、颜色、内存大小 方法:打游戏、写代码、看视频a.创建电脑类的对象,然后通过对象点...
    rzlong阅读 129评论 0 0
  • 时间关系 代码只基本实现题目基本要求 最后一题使用昨天作业模块快速实现 1. 建立一个汽车类Auto -包括轮胎个...
    ham731阅读 424评论 0 1
  • 1.声明一个电脑类: 属性:品牌、颜 、内存 法:打游戏、写代码、看视频a.创建电脑类的对象,然后通过对象点...
    葡萄柚子茶阅读 322评论 0 1