- 定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法:
a. 获取学生的姓名:getname()
b. 获取学生的年龄:getage()
c. 返回3门科目中最高的分数。get_course()
class Student:
def __init__(self, name, age, chinese: int, math: int, english: int):
self.name = name
self.age = age
if not isinstance(chinese, int):
raise ValueError
if not isinstance(math, int):
raise ValueError
if not isinstance(english, int):
raise ValueError
self.score = (chinese, math, english)
def getname(self):
return self.name
def getage(self):
return self.age
def get_course(self):
return max(self.score)
s = Student("周旺",10,50,60,70)
print(s.getname())
print(s.getage())
print(s.get_course())
- 建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等成员变量,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。
再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD等成员变量 覆盖加速 减速的方法
class Auto:
def __init__(self, weight, color="黑色", tire=4, speed=0):
self.tire = tire
self.color = color
self.weight = weight
self._speed = speed
@property
def speed(self):
return "%d km/h" % self._speed
def speed_up(self):
self._speed += 10
def speed_down(self):
if self._speed!=0:
self._speed -= 10
def park(self):
self._speed = 0
auto1 = Auto(130)
auto2 = auto1.__class__(123)
print(auto2.weight)
print(auto1.speed)
auto1.speed_up()
auto1.speed_up()
auto1.speed_up()
print(auto1.speed)
auto1.speed_down()
auto1.speed_down()
print(auto1.speed)
auto1.park()
print(auto1.speed)
class CarAuto(Auto):
def __init__(self, weight, air_conditioner="无", cd="无", color="黑色", tire=4, speed=0):
super().__init__(weight, color, tire, speed)
self.air_conditioner = air_conditioner
self.cd = cd
def speed_up(self):
self._speed += 20
def speed_down(self):
self._speed -= 20
car = CarAuto(100)
print("========")
print(car.speed)
car.speed_up()
car.speed_up()
car.speed_up()
print(car.speed)
car.speed_down()
car.speed_down()
print(car.speed)
car.park()
print(car.speed)
创建一个名为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, phone, addr):
self.firstname = firstname
self.lastname = lastname
self.phone = phone
self.addr = addr
def describeuser(self):
print("我叫%s·%s ,住在%s,电话为:%s" % (self.firstname, self.lastname, self.addr, self.phone))
def greetuser(self):
print("您好,%s·%s" % (self.firstname, self.lastname))
class Admin(User):
def __init__(self, firstname, lastname, phone, addr, privileges: list):
super().__init__(firstname, lastname, phone, addr)
self.privileges = privileges
def show_privileges(self):
for p in self.privileges:
print(p)
admin = Admin("john","zhou","18048673649","玉林东路",["can add post","can delete post","can ban user"])
admin.describeuser()
admin.greetuser()
admin.show_privileges()
- 创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
getrefcount = 0
def __init__(self):
self.__class__.getrefcount += 1
print(self.__class__.getrefcount)
def __del__(self):
self.__class__.getrefcount -= 1
print(self.__class__.getrefcount)
del self
a = Person()
b = Person()
c = Person()
print("=========")
print(Person.getrefcount)
print("=========")
del a
del b
del c
- (尝试)写一个类,其功能是: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]蓝莲花
import re
import time
class Resolver:
def __init__(self, dir):
self.lyric = ""
self.lyric_list = {}
with open(dir, "r", encoding="utf-8") as file:
# 处理文件数据,生成二维数组
s_list = file.readlines()
for s in s_list:
time_list = re.findall("\[.+?\]", s)
s = re.sub("\[.+\]", "", s[:-1], count=0, flags=0)
for t in time_list:
self.lyric_list[t[1:-1]] = s
for s in s_list:
# s = re.sub("\[.+\]", "", s, count=0, flags=0)
self.lyric += s
def read_file(self):
return self.lyric
def play(self):
index_list = sorted(self.lyric_list)
for key in index_list:
print("[" + key + "]", self.lyric_list[key])
def auto_play(self):
def str2secend(str):
m = int(str[:2])
s = float(str[3:])
return m * 60 + s
index_list = sorted(self.lyric_list)
# for key in index_list-1:
index = 0
while True:
key = index_list[index]
print("[" + key + "]", self.lyric_list[key])
if index == len(index_list) - 1:
break
time.sleep(str2secend(index_list[index+1])-str2secend(index_list[index]))
index += 1
resolver = Resolver("./lyric.txt")
print(resolver.read_file())
# resolver.play()
resolver.auto_play()