0.定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法: a. 获取学生的姓名:getname() b. 获取学生的年龄:getage()
c. 返回3门科目中最高的分数。get_course()
class Student:
def __init__(self,name,age,chinese,math,english):
self.__name = name
self.__age = age
self._chinese = chinese
self._math = math
self._english = english
def getName(self):
return self.__name
def getage(self):
return self.__age
def get_course(self):
return max(self._chinese,self._english,self._math)
@property
def chinese(self):
return self._chinese
@chinese.setter
def chinese(self,chinese):
if isinstance(chinese,int):
raise ValueError
if not 0<=chinese<=100:
raise ValueError
self._chinese = chinese
@property
def math(self):
return self._math
@math.setter
def math(self, math):
if isinstance(math, int):
raise ValueError
if not 0 <= math <= 100:
raise ValueError
self._math = math
@property
def english(self):
return self._english
@english.setter
def english(self, english):
if isinstance(english, int):
raise ValueError
if not 0 <= english <= 100:
raise ValueError
self._english = english
student = Student("老刘",20,80,85,75)
print(student.chinese)
print(student.getName())
print(student.getage())
print(student.get_course())
1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等成员变量,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD等成员变量 覆盖加速 减速的方法
class Auto:
def __init__(self,tyre,color,weight,speed):
self.tyre = type
self.color = color
self.weight = weight
self.speed = speed
def speedUp(self):
self.speed += 1
return '速度+1'
def speedDown(self):
self.speed -= 1
return '速度-1'
def speedStop(self):
self.speed = 0
return '停止'
class CarAuto(Auto):
def __init__(self,tyre,color,weight,speed,airAonditioner,cd):
super().__init__(tyre,color,weight,speed)
self.airAonditioner = airAonditioner
self.cd = cd
carAuto = CarAuto(5,'white','2000kg',60,'美的','飞翔')
print(carAuto.speedUp())
print(carAuto.speedDown())
print(carAuto.speedStop())
print(carAuto.airAonditioner)
print(carAuto.cd)
2.创建一个名为User 的类,其中包含属性firstname 和lastname ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名 为describeuser() 的方法,它打印用户信息摘要;再定义一个名为greetuser() 的方法,它向用户发出个性化的问候。
管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承User类。添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post"、"can delete post"、"can ban user"等)组成的列表。编写一个名为show_privileges()的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。
class User:
def __init__(self,firstname,lastname,age,hobby,university):
self.firstname = firstname
self.lastname = lastname
self.age = age
self.hobby = hobby
self.university = university
def describeuser(self):
return self.lastname+'曾经用过:'+self.firstname+'的名字,今年'+str(self.age)+'岁,喜欢'+self.hobby+',毕业于'+self.university
def greetuser(self):
return '亲爱的'+self.lastname+'你好,听说'+self.hobby+'的人都很帅~~~'
class Admin(User):
def __init__(self,firstname,lastname,age,hobby,university,privileges=["can add post","can delete post","can ban user"]):
super().__init__(firstname,lastname,age,hobby,university)
self.privileges = privileges
def show_privileges(self):
return self.privileges
admin = Admin('Randy','Wildpigger',21,'唱歌','小野猪野生学院',["can add post","can delete post"])
print(admin.describeuser())
print(admin.greetuser())
print(admin.show_privileges())
3.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
num = 0
def __init__(self,name):
Person.num += 1
self.name = name
print(self.name+'是第'+ str(Person.num) +'个创建的对象~~~')
@classmethod
def personNums(self):
return '总共'+str(Person.num)+'个人~~'
p1 = Person('老刘')
p2 = Person('老曾')
p3 = Person('老余')
print(Person.personNums())
(尝试)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]才发觉脚下的路
[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 IrcAnalysis:
def __init__(self,MusicIrc:str):
self.MusicIrc = MusicIrc
def Analysis(self):
'''
:return:分析后的歌词
'''
self.line = self.MusicIrc.split('\n')
self.new_line=[]
for i in range(len(self.line)):
temp = self.line[i][::-1].split(']',1)
temp[0] = temp[0][::-1]
temp[1] = temp[1][::-1].lstrip('[').split('][')
for j in range(len(temp[1])):
item = temp[1][j].split(':')
item = float(item[0])*60 + float(item[1])
line=[float('%.2f'%item),temp[0]]
self.new_line.append(line)
self.new_line.sort(key=lambda x:x[0])
return self.new_line
def timeChange(self,time):
'''
:param time:以秒的形式输入:直接输入数值,如:10,12.5,16.05 或者以'02:06.05'形式输入
:return: 标准时间
'''
time = str(time).split(':')
if len(time)>1:
time = float(time[0])*60+float(time[1])
return time
else:
return float(time[0])
def getNowIrc(self,time):
'''
:param time:以秒的形式输入:直接输入数值,如:10,12.5,16.05 或者以'02:06.05'形式输入
:return: 当前歌词
'''
self.Analysis()
time = self.timeChange(time)
if time < self.new_line[0][0]:
return 'None'
for i in range(len(self.new_line)):
if self.new_line[i][0] <= time < self.new_line[i+1][0]:
return self.new_line[i][1]
irc = '''[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]蓝莲花'''
musicIrc = IrcAnalysis(irc)
print(musicIrc.getNowIrc('02:00.05'))