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)
day14-homework
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等属性 ==========Auto======...
- 时间关系 代码只基本实现题目基本要求 最后一题使用昨天作业模块快速实现 1. 建立一个汽车类Auto -包括轮胎个...