0.定義一個(gè)學(xué)生類。有屬性:姓名、年齡、成績(語文,數(shù)學(xué),英語)[每課成績的類型為整數(shù)]
方法: a. 獲取學(xué)生的姓名:getname() b. 獲取學(xué)生的年齡:getage()
c. 返回3門科目中最高的分?jǐn)?shù)。get_course()
class Score:
def __init__(self,chinese,math,english):
self.chinese = int(chinese)
self.math = int(math)
self.english = int(english)
class Student:
def __init__(self,name,age):
self.name = name
self.age = age
self.scores = []
def get_name(self):
print('姓名:%s'%self.name)
def get_age(self):
print('年齡:%s'%self.age)
def get_course(self):
score = Score(66,77,88)
new_score = score.__dict__
self.scores.append(new_score)
return max(score.english,score.math,score.chinese)
stu1 = Student('張三',18)
stu1.get_name()
stu1.get_age()
print(stu1.get_course())
1.建立一個(gè)汽車類Auto,包括輪胎個(gè)數(shù),汽車顏色,車身重量,速度等成員變量,
并通過不同的構(gòu)造方法創(chuàng)建實(shí)例。至少要求 汽車能夠加速 減速 停車。
再定義一個(gè)小汽車類CarAuto 繼承Auto 并添加空調(diào)、CD等成員變量 覆蓋加速 減速的方法
class Auto:
def __init__(self,tires,color,weight,speed):
self.tyre = tires
self.color = color
self.weight = weight
self.speed = speed
@staticmethod
def speed_up():
print('加速')
@staticmethod
def slow_down():
print('減速')
@staticmethod
def stop():
print('停車')
class CarAuto(Auto):
def __init__(self,air_conditioner,CD,tires,color,weight,speed):
super().__init__(tires,color,weight,speed)
self.air_conditioner=air_conditioner
self.CD=CD
@staticmethod
def speed_up():
print('我是CarAuto,正在加速!')
@staticmethod
def slow_down():
print('我是CarAuto,正在減速!')
p1=CarAuto('美的','天空之城',4,'黑色','1kg','80km/h')
print(p1.__dict__)
2.創(chuàng)建一個(gè)名為User 的類,其中包含屬性firstname 和lastname ,還有用戶簡介通常會存儲的其他幾個(gè)屬
性。在類User 中定義一個(gè)名 為describeuser() 的方法,它打印用戶信息摘要;
再定義一個(gè)名為greetuser() 的方法,它向用戶發(fā)出個(gè)性化的問候。
管理員是一種特殊的用戶。編寫一個(gè)名為Admin 的類,讓它繼承User類。
添加一個(gè)名為privileges 的屬性,用于存儲一個(gè)由字符串
(如"can add post"、"can delete post"、"can ban user"等)組成的列表。
編寫一個(gè)名為show_privileges()的方法,它顯示管理員的權(quán)限。創(chuàng)建一個(gè)Admin 實(shí)例,并調(diào)用這個(gè)方法。
class User:
def __init__(self,age,sex):
self.firstname = '張三'
self.lastname = '李四'
self.age= age
self.sex =sex
def describeuser(self):
print(self.__dict__)
@staticmethod
def greetuser():
print('你們好呀么么噠')
3.創(chuàng)建一個(gè)Person類,添加一個(gè)類字段用來統(tǒng)計(jì)Perosn類的對象的個(gè)數(shù)
class Person():
number = 0
def __init__(self):
Person.number += 1
p1 = Person()
p2 = Person()
print(Person.number)
(嘗試)5.寫一個(gè)類,其功能是:
1.解析指定的歌詞文件的內(nèi)容
2.按時(shí)間顯示歌詞 提示:歌詞文件的內(nèi)容一般是按下面的格式進(jìn)行存儲的。
歌詞前面對應(yīng)的是時(shí)間,在對應(yīng)的時(shí)間點(diǎn)可以顯示對應(yīng)的歌詞
'''
class Lyric:
def __init__(self,time,word):
self.time=time
self.word=word
def __gt__(self, other):
return self.time > other.time
class Analysis:
def __init__(self):
self.all_lyric=[]
self.collect_lyric()
def get_time_word(self,content):
contents=content.split(']')
word=contents[-1]
for time in contents[:-1]:
times = time[1:].split(':')
fen = float(times[0])
miao = float(times[1])
new_time = fen*60 + miao
lyric=Lyric(new_time,word)
self.all_lyric.append(lyric)
def collect_lyric(self):
try:
with open('./藍(lán)蓮花.txt','r',encoding='utf-8') as f:
line=f.readline()
while line:
self.get_time_word(line)
line=f.readline()
self.all_lyric.sort(reverse=True)
except FileNotFoundError:
print('文件不存在')
def get_word(self,time):
for lyric in self.all_lyric:
if lyric.time <=time:
return lyric.word
an1 = Analysis()
print(an1.get_word(17))