面向对象2-homework

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

class Auto:
    
    def __init__(self, number, color, weigth, rate):
        if number == 4 or number == 6 or number ==10:
            self._number = number
        else:
            raise ValueError
        self._color = color
        if (isinstance(weigth, int) or isinstance(weigth, float)) and weigth > 0:
            self._weight = weigth
        else:
            raise ValueError
        if rate >= 0:
            self._rate = rate
        else:
            raise ValueError

    @property
    def number(self):
        return self._number

    @number.setter
    def number(self, value):
        if value == 4 or value == 6 or value ==10:
            self._number = value
        else:
            raise ValueError

    @property
    def weigth(self):
        return self._weigth

    @number.setter
    def weigth(self, value):
        if (isinstance(value, int) or isinstance(value, float)) and value > 0:
            self._weigth = value
        else:
            raise ValueError

    @property
    def rate(self):
        return self._rate

    @number.setter
    def rate(self, value):
        if value >= 0:
            self._rate = value
        else:
            raise ValueError

    def slow_down(self):
        speed = 15
        while True:
            time.sleep(1)
            if self._rate < 60:
                print('当前速度为{},速度不够请加速'.format(self._rate))
                break
            if  speed == 0:
                print('汽车正在以每秒{}m的速度行驶'.format(self._rate))
                break
            self._rate -= speed
            print('汽车正在以每秒{}m的速度减速'.format(speed))
            print('当前速度为{}'.format(self._rate))
            speed -=1
            if speed <= 0:
                speed = 0

    def to_speed_up(self):
        speed = 1
        while True:
            time.sleep(1)
            if self._rate > 120:
                print('当前速度为{},已超速,请减速'.format(self._rate))
                break
            self._rate += speed
            print('汽车正在以每秒{}m的速度加速'.format(speed))
            print('当前速度为{}'.format(self._rate))
            speed +=3
            
    def stop(self):
        speed = 22
        while True:
            time.sleep(1)
            self._rate -= speed
            if self._rate <= 0:
                self._rate = 0
                print('当前速度为{}汽车已经停车'.format(self._rate))
                break
            print('汽车正在以每秒{}m的速度减速'.format(speed))
            print('当前速度为{}'.format(self._rate))
            speed -= 2


class CarAuto (Auto):
    def __init__(self, number, color, weigth, rate, air_conditioner=None, music=None):
        super().__init__(number, color, weigth, rate)
        self.air_conditioner = air_conditioner
        self.music = music

    def slow_down(self):
        speed = 15
        while True:
            time.sleep(1)
            if self._rate < 60:
                print('当前速度为{},速度不够请加速'.format(self._rate))
                break
            if  speed == 0:
                print('汽车正在以每秒{}m的速度行驶'.format(self._rate))
                break
            self._rate -= speed
            print('汽车正在以每秒{}m的速度减速'.format(speed))
            print('当前速度为{}'.format(self._rate))
            speed -=1
            if speed <= 0:
                speed = 0
            
    def to_speed_up(self):
        speed = 4
        while True:
            time.sleep(1)
            if self._rate >= 120:
                print('当前速度为{},已超速,请减速'.format(self._rate))
                break
            self._rate += speed
            print('汽车正在以每秒{}m的速度加速'.format(speed))
            print('当前速度为{}'.format(self._rate))
            speed +=2


def main(self):
    with open('./car.txt', 'r') as car_file:
        page = car_file.read()
    while True:
        print(page)
        case = int(input('请选择功能'))
        if case == 1:
            self.to_speed_up()
        elif case == 2:
            self.slow_down()
        elif case == 3:
            self.stop()
        elif case == 4:
            quirt()
        else:
            print('输入有误')
            main(self)

def quirt():
    exit()


if __name__ == '__main__':

    car = CarAuto(4, '黑色', 2000, 80)
    main(car)
面向对象2-homework_第1张图片
1.PNG

面向对象2-homework_第2张图片
2.PNG
  1. 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person(object):
    """docstring for Person"""
    people = 0

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


p1 = Person('小陈', 18, '女')
p2 = Person('小李', 19, '男')
p3 = Person('小王', 20, '男')
print(Person.people)

  1. 创建一个动物类,拥有属性:性别、年龄、颜色、类型 ,

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

class Animal():
    """docstring for Animal"""

    def __init__(self, mold, age, color, sex):
        self.sex = sex
        self.age = age
        self.color = color
        self.mold = mold

    def show(self):
        print('{}的对象:类型为{}, 年龄为{},颜色为{},性别为{}'.format(Animal.__name__, self.mold, self.age, self.color, self.sex))


panda = Animal('熊猫', 3, '黑白', '公')
panda.show()  
  1. 写一个圆类, 拥有属性半径、面积和周长;要求获取面积和周长的时候的时候可以根据半径的值把对应的值取到。但是给面积和周长赋值的时候,程序直接崩溃,并且提示改属性不能赋值
from math import pi


class CircleValueError(Exception):
    """docstring for CircleValueError"""
    def __str__(self):
        return '该属性不能被赋值'
        

class Circle(object):
    """docstring for Circle"""
    def __init__(self, radius, area=None, permeter=None):
        if (isinstance(radius, int) or isinstance(radius, float)) and radius > 0:
            self._radius = radius
        else:
             raise ValueError
        self._area = area
        self._permeter = permeter

    @property
    def radius(self):
        return self._radius
    
    @radius.setter
    def radius(self, value):
        if (isinstance(value, int) or isinstance(value, float)) and value > 0:
            self._radius = value
        else:
            raise ValueError

    @property
    def area(self):
        return pi * self._radius ** 2

    @area.setter
    def area(self, value):
        raise CircleValueError

    @property
    def permeter(self):
        return 2 * pi * self._radius

    @permeter.setter
    def permeter(self, value):
        raise CircleValueError


circle = Circle(3)
print(circle.area)
print(circle.permeter)
circle.area = 10
  1. 写一个扑克类, 要求拥有发牌和洗牌的功能(具体的属性和其他功能自己根据实际情况发挥)
import random
import time


class Poker(object):
    """docstring for Poker"""
    def __init__(self):
        self.design_color = ['♥', '♠', '♣', '♦']
        self.points = ['2', '3', '4','5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
        self.king = ['大王', '小王']
        self.deck = [i+j for i in self.points for j in self.design_color]
        self.deck.extend(self.king)
        self.player_1 = []
        self.player_2 = []
        self.player_3 = []
        self.card = []

    def shuffle(self):
        print('正在洗牌中......')
        time.sleep(5)
        random.shuffle(self.deck)
        
    def deal(self):
        self.player_1.extend(self.deck[0:2])
        self.player_2.extend(self.deck[2:4])
        self.player_3.extend(self.deck[4:6])
        self.card.extend(self.deck[51:])
        for i in range(5):
            self.player_1.extend(self.deck[6 + i * 9: 6 + i * 9 + 3])
            self.player_2.extend(self.deck[9 + i * 9: 9 + i * 9 + 3])
            self.player_3.extend(self.deck[12 + i * 9: 12 + i * 9 + 3])
        num = random.randint(1, 14)
        if num % 3 == 1:
            # self.player_1.extend(self.card)
            print('恭喜player_1成为地主')
        if num % 3 == 2:
            self.player_2.extend(self.card)
            print('恭喜player_2成为地主')
        if num % 3 == 0:
            self.player_3.extend(self.card)
            print('恭喜player_3成为地主')
        time.sleep(2)
        print('正在发牌中......')
        time.sleep(5)
        print(self.player_1)
        print(self.player_2)
        print(self.player_3)


p = Poker()
p.shuffle()
p.deal()
  1. (尝试)写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词

    [00:00.20]蓝莲花   
    [00:00.80]没有什么能够阻挡   
    [00:06.53]你对自由地向往   
    [00:11.59]天马行空的生涯  
    [00:16.53]你的心了无牵挂   
    [02:11.27][01:50.22][00:21.95]穿过幽暗地岁月   
    [02:16.51][01:55.46][00:26.83]也曾感到彷徨   
    [02:21.81][02:00.60][00:32.30]当你低头地瞬间  
    [02:26.79][02:05.72][00:37.16]才发觉脚下的路   
    [02:32.17][00:42.69]心中那自由地世界  
    [02:37.20][00:47.58]如此的清澈高远   
    [02:42.32][00:52.72]盛开着永不凋零   
    [02:47.83][00:57.47]蓝莲花  
    

你可能感兴趣的:(面向对象2-homework)