0.定義?個學?類椒惨。有屬性:姓名系洛、年齡、成績(語?蝇狼,數(shù)學,英語)[每課成績的類型為整數(shù)]
?法: a. 獲取學?的姓名:getname() b. 獲取學?的年齡:getage()
c. 返回3?科?中最?的分數(shù)欣硼。get_course()
class Student:
"""學生類"""
def __init__(self, name, age):
self.name = name
self.age = age
self.scores = {'chinese': 80, 'math': 85, 'english': 75}
def get_name(self):
return self.name
def get_age(self):
return self.age
def get_course(self):
max_score = self.scores['chinese']
for score in self.scores.values():
if max_score < score:
max_score = score
return max_score
if __name__ == '__main__':
joe = Student('jerry', 18)
print(joe.get_name())
print(joe.get_age())
print(joe.get_course())
結果:
jerry
18
85
1.建??個汽?類Auto题翰,包括輪胎個數(shù)恶阴,汽?顏?诈胜,?身重量,速度等成員變量冯事,并通過不同的構造?法創(chuàng)
建實例焦匈。?少要求 汽?能夠加速 減速 停?。 再定義?個?汽?類CarAuto 繼承Auto 并添加空調昵仅、CD等成
員變量 覆蓋加速 減速的?法
class Auto:
"""汽車類"""
def __init__(self, wheels=4, color='', weight=100, speed=0):
self.wheels = wheels
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self, speed):
self.speed += speed
def slow_down(self, speed):
if self.speed > speed:
self.speed -= speed
else:
self.speed = 0
print('減不下去了')
def stop(self):
self.speed = 0
class CarAuto(Auto):
"""小汽車類"""
def __init__(self, wheels=4, color='', weight=100, speed=0, ac='', cd=''):
super().__init__(wheels, color, weight, speed)
self.ac = ac
self.cd = cd
def speed_up(self, speed):
self.speed += speed
def slow_down(self, speed):
if self.speed > speed:
self.speed -= speed
else:
self.speed = 0
print('減不下去了')
def stop(self):
self.speed = 0
if __name__ == '__main__':
pass
2.創(chuàng)建?個名為User 的類缓熟,其中包含屬性firstname 和lastname ,還有?戶簡介通常會存儲的其他?個屬
性摔笤。在類User 中定義?個名 為describeuser() 的?法够滑,它打印?戶信息摘要;再定義?個名為greetuser() 的
?法,它向?戶發(fā)出個性化的問候吕世。
管理員是?種特殊的?戶彰触。編寫?個名為Admin 的類,讓它繼承User類命辖。添加?個名為privileges 的屬性况毅,
?于存儲?個由字符串(如"can add post"、"can delete post"尔艇、"can ban user"等)組成的列表尔许。編寫?個名為
show_privileges()的?法,它顯示管理員的權限终娃。創(chuàng)建?個Admin 實例味廊,并調?這個?法。
class User:
"""用戶類"""
def __init__(self, first_name, last_name, description, hometown):
self.first_name = first_name
self.last_name = last_name
self.description = description
self.hometown = hometown
def describe_user(self):
return '我是%s%s,來自%s,%s' % (self.last_name, self.first_name, self.hometown, self.description)
class Admin(User):
"""管理員類"""
def __init__(self, first_name, last_name, description, hometown, privilege=''):
super().__init__(first_name, last_name, description, hometown)
self.privilege = privilege
def show_privileges(self):
return '%s' % self.privilege
if __name__ == '__main__':
pass
3.創(chuàng)建?個Person類,添加?個類字段?來統(tǒng)計Perosn類的對象的個數(shù)
class Person:
"""人類"""
all_person = []
def __init__(self, name, age, sex):
self.name = ''
self.age = 0
self.sex = ''
@classmethod
def add_person(cls):
name = input('name:')
age = input('age:')
sex = input('sex:')
person = Person(name, age, sex)
cls.all_person.append(person)
if __name__ == '__main__':
pass
(嘗試)5.寫?個類余佛,其功能是: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]才發(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]藍蓮花
import pygame
lyrics = {'藍蓮花': [1], '沒有什么能夠阻擋': [3], '你對?由地向往': [5, 9], '天??空的?涯': [6, 10]}
class Lyric:
"""歌詞類"""
song = []
def __init__(self, lyric, time):
self.lyric = lyric
self.time = time
@classmethod
def add_lyric(cls, lyrics):
for lyric,time in lyrics.items():
instance = Lyric(lyric, time)
cls.song.append(instance)
@classmethod
def play_lyric(cls):
timer = 0
is_play = cls.song[0].lyric
while True:
is_print = False
for lyric in cls.song:
if timer in lyric.time:
is_play = lyric.lyric
is_print = True
if is_print:
print(is_play)
pygame.time.delay(1000)
timer += 1
if timer == 10:
break
@classmethod
def find_lyric(cls, time):
timer = time
is_play = ''
is_print = False
flag = False
for _ in range(time, 0, -1):
for lyric in cls.song:
if timer in lyric.time:
is_play = lyric.lyric
is_print = True
flag = True
if flag:
break
timer -= 1
if is_print:
print(is_play)
if __name__ == '__main__':
Lyric.add_lyric(lyrics)
Lyric.play_lyric()
print('-'*20)
Lyric.find_lyric(8)
結果:
藍蓮花
沒有什么能夠阻擋
你對?由地向往
天??空的?涯
你對?由地向往
--------------------
天??空的?涯