DAY15 作業(yè) 屬性 繼承

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

class Auto:

    def __init__(self, tyre=4, color='white', weight=0, speed=0):
        self.tyre = tyre
        self.color = color
        self.weight = weight
        self.speed = speed

     def accelerate(self):
        speed_most = 150
        new_speed = self.speed + 20
        if new_speed <= speed_most:
            self.speed = new_speed
            print("已加速,當(dāng)前速度為%d" % self.speed)
        else:
            print('已經(jīng)加速到最大值,不能加速了!')

    def slow(self):
        new_speed = self.speed - 20
        if new_speed >= 0:
            self.speed = new_speed
            print("已減速,當(dāng)前速度為%d" % self.speed)
        else:
            print('不能減速')

    def stop(self):
        self.speed = 0
        print("已停車")



car1 = Auto()
car1.tyre = 4
car1.color = 'yellow'
car1.weight = '666'
car1.speed = 120
car1.slow()

car2 = Auto(4, 'black', 777, 140)
car2.accelerate()



class CarAuto(Auto):

    def __init__(self):
        super().__init__()
        self.air_conditioned = False
        self.CD = False

    def accelerate(self):
        self.speed += 30
        print("速度已經(jīng)提升至%d" % self.speed)

    def slow(self):
        self.speed -= 40
        print("速度已經(jīng)降至%d" % self.speed)


new_car1 = CarAuto()
new_car1.air_conditioned = True
new_car1.CD = True
new_car1.speed = 128
print(new_car1.air_conditioned)
new_car1.accelerate()
new_car1.slow()

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

class Person:
    count = 0

    def __init__(self):
        Person.count += 1

3.創(chuàng)建一個動物類,擁有屬性:性別、年齡、顏色、類型 ,
要求打印這個類的對象的時候以'/XXX的對象: 性別-? 年齡-? 顏色-? 類型-?/' 的形式來打印

class Animal:
    def __init__(self, gender='公', age=0, color='yellow', atype='dog'):
        self.gender = gender
        self.age = age
        self.color = color
        self.atype = atype

    def __str__(self):
        return str(self.__class__)+'的對象:性別-'+self.gender+',年齡-'+str(self.age)+',類型—'+self.atype


animal1 = Animal('母', 2, 'black', 'bird')
print(animal1)

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

class WriteError(Exception):
    def __str__(self):
        return '改屬性不能賦值!'


class Circle:
    def __init__(self, radius=0):
        self._radius = radius
        self._area = radius**2*math.pi
        self._perimeter = radius*math.pi*2

    @property
    def radius(self):
        return self._radius

    @radius.setter
    def radius(self, value):
        self._radius = value
        self._area = self._radius**2*math.pi
        self._perimeter = self._radius*math.pi*2

    @property
    def area(self):
        return self._area

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

    @property
    def perimeter(self):
        return self._perimeter

    @perimeter.setter
    def perimeter(self, value):
        raise WriteError


c1 = Circle(2)
print(c1.perimeter)

c1.radius = 3
print(c1.perimeter)

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

[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]當(dāng)你低頭地瞬間  
[02:26.79][02:05.72][00:37.16]才發(fā)覺腳下的路   
[02:32.17][00:42.69]心中那自由地世界  
[02:37.20][00:47.58]如此的清澈高遠   
[02:42.32][00:52.72]盛開著永不凋零   
[02:47.83][00:57.47]藍蓮花  
class Song:

    def __init__(self, path):
        self.path = path
        self.line = ''
        self.time = []

    def time_list(self):
        time_lists = []
        with open(self.path, encoding='utf-8') as f:
            line = f.readline()
            while line:
                lyric_list = line.split(']')
                for time_str in lyric_list[:-1]:
                    time_dict = {}
                    real_time = time_str.split('[')[1]
                    minute = int(real_time.split(':')[0])
                    second = float(real_time.split(':')[1])
                    all_second = minute*60+second
                    time_dict['time'] = all_second
                    time_dict['lyric'] = lyric_list[-1]
                    time_lists.append(time_dict)
                line = f.readline()
            time_lists.sort(key=lambda x: x['time'])
            return time_lists

    def analysis_lyric(self, time=0):
        time_lists = self.time_list()
        for time_dic in time_lists:
            if time_dic['time'] < time:
                text = time_dic['lyric']
            else:
                break
        return text


s1 = Song('lyric.txt')
print(s1.analysis_lyric(160))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • 建立一個汽車類Auto,包括輪胎個數(shù),汽車顏色,車身重量,速度等屬性,并通過不同的構(gòu)造方法創(chuàng)建實例。至少要求 汽車...
    __e145閱讀 282評論 0 0
  • 1.建立一個汽車類Auto,包括輪胎個數(shù),汽車顏色,車身重量,速度等屬性,并通過不同的構(gòu)造方法創(chuàng)建實例。至少要求 ...
    664a159048ed閱讀 111評論 0 0
  • 1.建立一個汽車類Auto,包括輪胎個數(shù),汽車顏色,車身重量,速度等屬性,并通過不同的構(gòu)造方法創(chuàng)建實例。 至少要求...
    不語sun閱讀 234評論 0 0
  • 8月22日-----字符串相關(guān) 2-3 個性化消息: 將用戶的姓名存到一個變量中,并向該用戶顯示一條消息。顯示的消...
    future_d180閱讀 1,042評論 0 1
  • ——《整理藝術(shù)3》書評 在這個物質(zhì)極大豐富的時代,塞滿家里的不止是吃、穿、用、行等等亂七八糟的東西,緊隨其后的就是...
    藝馮閱讀 497評論 0 1

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