day15作業(yè)

1. 建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù),汽車顏色,車身重量,速度等屬性,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 汽車能夠加速 減速 停車。 再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)、CD屬性,并且重新實(shí)現(xiàn)方法覆蓋加速、減速的方法

class Auto:
    def __init__(self):
        self.tire_num = 2
        self.car_color = 'black'
        self.car_weight = '150kg'
        self.speed = '20km/h'

    def speed_up(self):
        return (self.car_color, str(self.tire_num)+'個(gè)輪子', self.car_weight+'的汽車'+'加速')

    def speed_cut(self):
        return (self.car_color, str(self.tire_num)+'個(gè)輪子', self.car_weight+'的汽車'+'減速')

    def stop_car(self):
        return (self.car_color, str(self.tire_num)+'個(gè)輪子', self.car_weight+'的汽車'+'停車')


class CarAuto(Auto):
    def __init__(self, air_condition, CD):
        super().__init__()
        self.air_conditon = air_condition
        self.CD = CD

    def speed_up(self):
        return (self.CD,self.air_conditon,self.car_color, str(self.tire_num)+'個(gè)輪子', self.car_weight+'的汽車'+'減速')

    def speed_cut(self):
        return (self.CD,self.air_conditon,self,self.car_color, str(self.tire_num)+'個(gè)輪子', self.car_weight+'的汽車'+'減速')

moto = Auto()
print(moto.__dict__)
print(moto.speed_cut())
print(moto.speed_up())
print(moto.stop_car())
car = CarAuto('30度', '成都')
print(car.__dict__)
print(car.speed_up())
print(car.speed_cut())

2. 創(chuàng)建一個(gè)Person類,添加一個(gè)類字段用來統(tǒng)計(jì)Perosn類的對(duì)象的個(gè)數(shù)

class Person:
    count = 0

    def __init__(self):  #創(chuàng)建一次對(duì)象自動(dòng)調(diào)用魔法方法一次
        Person.count += 1


p1 = Person()
p2 = Person()
p3 = Person()
print(Person.count)

3. 創(chuàng)建一個(gè)動(dòng)物類,擁有屬性:性別、年齡、顏色、類型 ,

要求打印這個(gè)類的對(duì)象的時(shí)候以'/XXX的對(duì)象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印

class Animal:
    def __init__(self):
        self.sex = '-?'
        self.age = '-?'
        self.color = '-?'
        self.type = '-?'
a1 = Animal()
print('/'+str(a1.__class__)[17:-2],'對(duì)象:'+'性別'+a1.sex+'年齡'+a1.age+'顏色'+a1.color+'類型'+a1.type+'/')

方法2:

class Animal:
    def __init__(self):
        self.sex = '?'
        self.age = '?'
        self.color = '?'
        self.type = '?'

    def __str__(self):
        return '/{}對(duì)象:性別-{} 年齡-{} ' \
               '類型-{}/'.format(str(self.__class__)[17:-2], self.sex, self.age, self.color, self.type)

a1 = Animal()
print(a1)

4. 寫一個(gè)圓類, 擁有屬性半徑、面積和周長(zhǎng);要求獲取面積和周長(zhǎng)的時(shí)候的時(shí)候可以根據(jù)半徑的值把對(duì)應(yīng)的值取到。但是給面積和周長(zhǎng)賦值的時(shí)候,程序直接崩潰,并且提示改屬性不能賦值

class ValueError(Exception):
    def __str__(self):
        return '該方法不能重寫'

class Circle:
    pi = 3.1415926

    def __init__(self, r):
        self.r = r
        self._area = 0
        self._perimeter = 0

    @property
    def area(self):
        return Circle.pi*self.r*self.r

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

    @property
    def perimeter(self):
        return 2*Circle.pi*self.r

    @perimeter.setter
    def perimeter(self):
        raise ValueError



p1 = Circle(2)
print(p1.area)
# p1.area = 100
# print(p1.area)
p1.perimeter = 100
print(p1.perimeter)

5. 寫一個(gè)撲克類, 要求擁有發(fā)牌和洗牌的功能(具體的屬性和其他功能自己根據(jù)實(shí)際情況發(fā)揮)

import random
class Poker:
    count = 1
    @staticmethod
    def born_poker():
        list1 = []
        color = ['黑桃', '梅花', '紅桃', '方塊']
        num = list(range(2, 11))
        num1 = ('J', 'Q', 'K', 'A')
        num.extend(num1)
        for i in color:
            for j in num:
                list1.append([i,j])
                print(i,j, end=' ')
        list1.extend(['大王', '小王'])
        return list1

    def __init__(self,list1):
        self.list1 = list1

    def shuffle(self):
        random.shuffle(self.list1)
        return self.list1

    def deal1(self):
        print('\n==========第%s局=========' % Poker.count)
        print(p1.shuffle())
        print('\n底牌:', self.list1[:3])
        print('第一個(gè)人的牌:', self.list1[3:20])
        print('第二個(gè)人的牌:', self.list1[20:37])
        print('第三個(gè)人的牌:', self.list1[37:55])
        Poker.count += 1

    def richcard_card(self, play_poker=True):
        if not play_poker:
            return self.shuffle(), self.deal1()
        else:
            return self.deal1()


p1 = Poker(Poker.born_poker())
p1.born_poker()
p1.deal1()
p1.richcard_card()

6. (嘗試)寫一個(gè)類,其功能是:1.解析指定的歌詞文件的內(nèi)容 2.按時(shí)間顯示歌詞 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲(chǔ)的。歌詞前面對(duì)應(yīng)的是時(shí)間,在對(duì)應(yīng)的時(shí)間點(diǎn)可以顯示對(duì)應(yīng)的歌詞

import time


class Song:
    def __init__(self, minute, second, millisecond):
        self.minute = minute
        self.second = second
        self.millisecond = millisecond
        self.time1 = None
        self.lyric = None

    @staticmethod
    def read_lyric():  #這個(gè)對(duì)象方法負(fù)責(zé)讀文本內(nèi)容按行讀
        with open('./lanlianhua', 'r', encoding='utf-8') as f:
            return f.readlines()

    def print_lyric(self):
        while self.minute < 3:
            self.millisecond += 1
            if self.millisecond == 60:
                self.second += 1
                self.millisecond = 0
            elif self.second == 60:
                self.minute += 1
                self.second = 0
            self.time1 = '['+str(self.minute).rjust(2, '0')+':'+str(self.second).rjust(2, '0')\
                         + '.'+str(self.millisecond).rjust(2, '0')+']'
            print(self.time1)    #每微妙開始讀
            for self.lyric in Song.read_lyric():
                if self.time1 in self.lyric[:self.lyric.rfind(']')+1]:    #讀到與文本相同時(shí)間歌詞就打印
                    print(self.lyric[self.lyric.rfind(']')+1:])
                    time.sleep(0.8)
                    break


p1 = Song(0, 0, 0)
p1.print_lyric()
[00:00.20]藍(lán)蓮花   
[00:00.80]沒有什么能夠阻擋   
[00:06.53]你對(duì)自由地向往   
[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]當(dāng)你低頭地瞬間  
[02:26.79][02:05.72][00:37.16]才發(fā)覺腳下的路   
[02:32.17][00:42.69]心中那自由地世界  
[02:37.20][00:47.58]如此的清澈高遠(yuǎn)   
[02:42.32][00:52.72]盛開著永不凋零   
[02:47.83][00:57.47]藍(lán)蓮花  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù),汽車顏色,車身重量,速度等屬性,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 汽車...
    ______n___閱讀 175評(píng)論 0 0
  • day15-作業(yè) 1. 建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù),汽車顏色,車身重量,速度等屬性,并通過不同的構(gòu)造方法...
    Octane閱讀 187評(píng)論 0 1
  • 1.建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù),汽車顏色,車身重量,速度等屬性,并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 ...
    oct___越來越2閱讀 322評(píng)論 0 0
  • 小蟻為何改名NEO? NEO這個(gè)項(xiàng)目是達(dá)鴻飛的團(tuán)隊(duì)在2014年做的, 他在2011年開始接觸比特幣,13年開始全職...
    每日區(qū)塊先知閱讀 473評(píng)論 0 0
  • 睡醒的時(shí)候已經(jīng)是七點(diǎn)零九分了,急急忙忙穿衣梳洗,平時(shí)需要四十分鐘做完的事竟然在慌亂中的十五分鐘內(nèi)完成了。拿鑰匙、鎖...
    范小羊閱讀 421評(píng)論 0 4

友情鏈接更多精彩內(nèi)容