0.定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]方法: a. 获取学生的姓名:getname() b. 获取学生的年龄:getage()c. 返回3门科目中最高的分数。get_course()
class Student:
def __init__(self,name,age):
self.name = name
self.age = age
self._course = 0
def get_name(self):
print("姓名:%s"%self.name)
def get_age(self):
print("姓名:%s,年龄:%s"%(self.name,self.age))
@property
def course(self):
print("最高分为:",self._course)
return self._course
@course.setter
def course(self,list1:list):
for item in list1:
item = int(item)
if not isinstance(item,int):
print("成绩必须是整数")
else:
max_value = max(list1)
max_index = list1.index(max_value)
if max_index == 0:
self._course = "语文"+ "最高"+str(max_value)
elif max_index == 1:
self._course = "数学"+ "最高"+str(max_value)
else:
self._course = "英语" + "最高"+str(max_value)
all_course = []
chinese_course = int(input("请输入语文分数:"))
all_course.append(chinese_course)
math_course = int(input("请输入数学分数:"))
all_course.append(math_course)
english_course = int(input("请输入英语分数:"))
all_course.append(english_course)
stu1 = Student("lorry",100)
stu1.get_age()
stu1.get_name()
stu1.course = all_course
stu1.course
class Grade:
def __init__(self,chiness:int,math:int,english:int):
self.chinese = chiness
self.math = math
self.english = english
def get_best_grade(self):
all_score = self.__dict__.values()
return max(all_score)
class Student:
def __init__(self,name,age,grade = Grade(0,0,0)):
self.name = name
self.age = age
self.grade = grade
def getname(self):
return self.name
def getage(self):
return self.age
def get_max_grade(self):
return self.grade.get_best_grade()
stu1 = Student("小明",18,Grade(chiness=70,math=90,english=50))
print(stu1.get_max_grade())
1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等成员变量,并通过不同的构造方法创建实例。至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD等成员变量 覆盖加速 减速的方法
class Auto:
def __init__(self,tire_num = 4,color = "yellow",weight = "4t",speed = 0):
self.tire_num = tire_num
self.color = color
self.weight = weight
self.speed = speed
def speed_up(self):
print("加速")
def speed_down(self):
print("减速")
def stop(self):
print("停车")
car1 = Auto()
car1.speed_down()
car2 = Auto()
car2.speed_up()
car3 = Auto()
car3.stop()
class CarAuto(Auto):
def __init__(self,tire_num ,color,weight,speed = "",air_condition = "",cd = ""):
super().__init__(tire_num,color,weight,speed)
self.air_condition = air_condition
self.cd = cd
def speed_up(self):
super().speed_up()
print("小汽车加速")
def speed_down(self):
super().speed_down()
print("小汽车减速")
scar = CarAuto(4,"green",20)
scar.speed_down()
class Auto:
def __init__(self,color = "白色",weight = 0):
self.tire = 4
self.color = color
self.weiht = weight
self.speed = 0
def speed_up(self,speed):
if self.speed + speed >= 240:
print("您已超速")
self.speed = 240
return
self.speed += speed
def speed_down(self,speed:int):
if self.speed - speed < 0:
self .speed = 0
return
self.speed -= 0
def stop(self):
self.speed = 0
class CarAuto(Auto):
def __init__(self,color = "",weight = 0,aircondition = "",cd = ""):
super().__init__(color,weight)
self.aircondition = aircondition
self.cd = cd
def speed_up(self,speed):
if self.speed + speed >= 260:
print("您已超速")
self.speed = 240
return
self.speed += speed
def speed_down(self,speed:int):
if self.speed - speed < 0:
self .speed = 0
return
self.speed -= 0
2.创建一个名为User 的类,其中包含属性firstname 和lastname ,还有用户简介通常会存储的其他几个属性。在类User 中定义一个名 为describeuser() 的方法,它打印用户信息摘要;再定义一个名为greetuser() 的方法,它向用户发出个性化的问候。
管理员是一种特殊的用户。编写一个名为Admin 的类,让它继承User类。添加一个名为privileges 的属性,用于存储一个由字符串(如"can add post"、"can delete post"、"can ban user"等)组成的列表。编写一个名为show_privileges()的方法,它显示管理员的权限。创建一个Admin 实例,并调用这个方法。
3.创建一个Person类,添加一个类字段用来统计Perosn类的对象的个数
class Person():
count = 0
def __init__(self,name):
self.name = 0
Person.count += 1
p1 = Person("lorry")
p2 = Person("lprrybz")
p3 = Person("alla")
print(Person.count)
5(尝试).写一个类,其功能是:1.解析指定的歌词文件的内容 2.按时间显示歌词 提示:歌词文件的内容一般是按下面的格式进行存储的。歌词前面对应的是时间,在对应的时间点可以显示对应的歌词
[00:00.20]蓝莲花 [00:00.80]没有什么能够阻挡 [00:06.53]你对自由地向往 [00:11.59]天马行空的生涯 [00:16.53]你的心了无牵挂 02:11.27[00:21.95]穿过幽暗地岁月 02:16.51[00:26.83]也曾感到彷徨 02:21.81[00:32.30]当你低头地瞬间 02:26.79[00:37.16]才发觉脚下的路 02:32.17心中那自由地世界 02:37.20如此的清澈高远 02:42.32盛开着永不凋零 02:47.83蓝莲花
100s -
网友评论