day16作业
0.定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法:
class Student:
def __init__(self, name, age=0, chinese=0, math=0, english=0):
self.name = name
self._age = age
self._chinese = chinese
self._math = math
self._english = english
self.score = {'chinese': self._chinese, 'math': self._math, 'english': self._english}
# 给age加条件(100岁以内且是整数)
# 先添加getter
@property
def age(self):
return self._age
# 给age添加setter
@age.setter
def age(self, value):
if not isinstance(value, int):
raise ValueError
if not 0 <= value <= 100:
raise ValueError
self._age = value
# 给chinese加条件(100分以内且是整数)
# 先添加getter
@property
def chinese(self):
return self._chinese
# 给chinese添加setter
@chinese.setter
def chinese(self, value):
if not isinstance(value, int):
raise ValueError
if not 0 <= value <= 100:
raise ValueError
self._chinese = value
# 给math加条件(100分以内且是整数)
# 先添加getter
@property
def math(self):
return self._math
# 给math添加setter
@math.setter
def math(self, value):
if not isinstance(value, int):
raise ValueError
if not 0 <= value <= 100:
raise ValueError
self._math = value
# 给english加条件(100分以内且是整数)
# 先添加getter
@property
def english(self):
return self._english
# 给english添加setter
@english.setter
def english(self, value):
if not isinstance(value, int):
raise ValueError
if not 0 <= value <= 100:
raise ValueError
self._english = value
stu1 = Student('小明')
stu1.age = 20
stu1.chinese = 50
stu1.math = 70
stu1.english = 80
a. 获取学生的姓名:getname()
# 获取学生的姓名:
def getname(self):
return self.name
print(stu1.getname())
b. 获取学生的年龄:getage()
# 获取学生的年龄:
def getage(self):
return self.age
print(stu1.getage())
c. 返回3门科目中最高的分数。get_course()
# 返回3门科目中最高的分数:
def get_course(self):
self.score = {'chinese': self._chinese, 'math': self._math, 'english': self._english}
max_c = self._chinese
max_n = 'chinese'
for subject in self.score:
if self.score[subject] > max_c:
max_c =self.score[subject]
max_n = subject
return max_n, max_c
print(stu1.get_course())
1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等成员变量,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。
再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD等成员变量 覆盖加速 减速的方法
class Auto:
def __init__(self, color, weight, speed, tyre_num=4):
self.tyre_num = tyre_num
self.color = color
self.weight = weight
self.speed = speed
def __add__(self, other):
return self.speed + other
def speed_up(self, num):
new_speed = self.speed + num
print('加速到%d' % (new_speed))
def __sub__(self, other):
return self.speed - other
def speed_down(self, num):
new_speed = self.speed - num
print('减速到%d' % (new_speed))
def stop(self):
stop = self.speed - self.speed
print('刹车')
class CarAuto(Auto):
def __init__(self, air_conditioner, cd, color, weight, speed, tyre_num=4):
super().__init__(color, weight, speed, tyre_num)
self.air_conditioner = air_conditioner
self.cd = cd
def speed_down(self, num):
new_speed = self.speed - num
print('减速到%d' % (new_speed))
def speed_up(self, num):
new_speed = self.speed + num
print('加速到%d' % (new_speed))
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, first_name, last_name, tag, enjoy, where_from):
self.first_name = first_name
self.last_name = last_name
self.tag = tag
self.enjoy = enjoy
self.where_from = where_from
self.name = self.first_name + '.' + self.last_name
def describe_user(self):
print("%s是来自%s的人,喜欢%s,有事无事会说上一句%s" % (self.name, self.where_from, self.enjoy, self.tag))
def greet_user(self):
print("你好! %s" % (self.name))
class Admin(User):
def __init__(self,privileges='can add post'):
self.__privileges = privileges
def show_privileges(self):
admin_password = 123456
password = int(input('请输入管理员密码:'))
if password == admin_password:
print('管理员欢迎您')
print(self._Admin__privileges)
else:
print('错误,您没有权限')
man = User('Jason', 'Statham', 'whatever', 'fitness', 'China')
man.describe_user()
man.greet_user()
man2 = Admin()
print(man2.show_privileges())
3.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person:
num = 0
def __init__(self, name):
self.name = name
Person.num += 1
@classmethod
def nums(cls):
return cls.num
p1 = Person('张三')
p2 =Person('李四')
p3 = Person('王二')
print(Person.nums())
(尝试)4.写一个类,其功能是: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]蓝莲花