0.定义一个学生类。有属性:姓名、年龄、成绩(语文,数学,英语)[每课成绩的类型为整数]
方法: a. 获取学生的姓名:getname() b. 获取学生的年龄:getage()
c. 返回3门科目中最高的分数。get_course()
class Student:
"""学生"""
def __init__(self, name: str, age: int=0):
self.__name = name
self.__age = age
self._chinese_score = 0
self._math_score = 0
self._english_score = 0
def get_name(self):
return self.__name
def get_age(self):
return self.__age
@property
def chinese_score(self):
return self._chinese_score
@chinese_score.setter
def chinese_score(self, score):
if not isinstance(score, int):
raise TypeError
if not 0<=score<=100:
raise ValueError
self._chinese_score = score
@property
def math_score(self):
return self._math_score
@math_score.setter
def math_score(self, score):
if not isinstance(score, int):
raise TypeError
if not 0 <= score <= 100:
raise ValueError
self._math_score = score
@property
def english_score(self):
return self._english_score
@english_score.setter
def english_score(self, score):
if not isinstance(score, int):
raise TypeError
if not 0 <= score <= 100:
raise ValueError
self._english_score = score
def get_course(self):
return max([self._chinese_score, self._math_score, self._english_score])
stu1 = Student('小明', 20)
print(stu1.get_name()) # 小明
print(stu1.get_age()) # 20
# stu1.chinese_score = 101 # ValueError
stu1.chinese_score = 90
stu1.math_score = 88
stu1.english_score = 92
print(stu1.get_course()) # 92
1.建立一个汽车类Auto,包括轮胎个数,汽车颜色,车身重量,速度等成员变量,并通过不同的构造方法创建实例。
至少要求 汽车能够加速 减速 停车。 再定义一个小汽车类CarAuto 继承Auto 并添加空调、CD等成员变量
覆盖加速 减速的方法
2.创建一个名为User 的类,其中包含属性firstname 和lastname ,还有用户简介通常会存储的其他几个属性。
在类User 中定义一个名 为describeuser() 的方法,它打印用户信息摘要;
再定义一个名为greetuser() 的方法,它向用户发出个性化的问候。
class User:
"""User类"""
def __init__(self, firstname, lastname, profession='战士', weapon='剑'):
self.firstname = firstname
self.lastname = lastname
self.profession = profession
self.weapon = weapon
self._ability = 0
def describe_user(self):
print('姓名:%s%s, 职业:%s, 武器:%s, 战斗力:%s'\
% (self.firstname, self.lastname, self.profession, self.weapon, self.ability))
@property
def ability(self):
return self._ability
@ability.setter
def ability(self, num):
if not isinstance(num, int):
raise TypeError
self._ability = num
def greet_user(self):
if self._ability <= 5:
print('战五渣%s%s,欢迎来到地狱模式!' % (self.firstname, self.lastname))
elif 6 <= self._ability <= 50:
print('新手%s%s,加油练级!' % (self.firstname, self.lastname))
elif 51 <= self._ability <= 100:
print('老手%s%s,祝您好运!' % (self.firstname, self.lastname))
elif 101 <= self._ability:
print('大神%s%s,您还差提鞋的吗?我们做朋友吧!' % (self.firstname, self.lastname))
user1 = User('余', '笑宇', '法师', '战斧')
user1.describe_user()
user1.greet_user()
user1.ability = 20
user1.describe_user()
user1.greet_user()
user1.ability = 80
user1.describe_user()
user1.greet_user()
user1.ability = 500
user1.describe_user()
user1.greet_user()
# 姓名:余笑宇, 职业:法师, 武器:战斧, 战斗力:0
# 战五渣余笑宇,欢迎来到地狱模式!
# 姓名:余笑宇, 职业:法师, 武器:战斧, 战斗力:20
# 新手余笑宇,加油练级!
# 姓名:余笑宇, 职业:法师, 武器:战斧, 战斗力:80
# 老手余笑宇,祝您好运!
# 姓名:余笑宇, 职业:法师, 武器:战斧, 战斗力:500
# 大神余笑宇,您还差提鞋的吗?我们做朋友吧!
管理员是一种特殊的用户。编写一个名为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 = name
Person.count += 1 # __init__调用一次,类字段count加1
p1 = Person('paul')
p2 = Person('hardon')
p3 = Person('anthony')
p4 = Person('gordon')
p5 = Person('tuck')
p6 = Person('kahuang')
print(Person.count) # 6
(尝试)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 Lyric:
"""歌词"""
def __init__(self):
self.words = ''
self.minite = '00'
self.second = '00.00'
class LyricFile:
"""歌词文件"""
def __init__(self):
self.lyrics = [] # 对象属性为歌词类Lyric类型的对象
# 定义读取文件的静态方法
@staticmethod
def get_lines():
with open('./files/lyric.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
return lines
# 定义解析文件,并返回一句句歌词对象的对象方法
def analysis(self):
lines = LyricFile.get_lines()
for line in lines:
str1 = line
str2 = str1.replace('[', '*')
str3 = str2.replace(']', '*')
list1 = str3.split('*')
words = list1[-1]
for item in list1[:-1]:
if item:
list2 = item.split(':')
minite = list2[0]
second = list2[1]
lyric1 = Lyric()
lyric1.words = words
lyric1.minite = minite
lyric1.second = second
self.lyrics.append(lyric1)
# 按照顺序打印歌词
def show_lyrics(self):
self.lyrics.sort(key= lambda item: float(item.minite)*60 + float(item.second))
for lyric in self.lyrics:
print(lyric.words)
# 定义一个对象方法,获取输入时间对应的歌词
def get_lyric_by_time_stamp(self, time_stamp: str):
list1 = time_stamp.split(':')
minite1 = list1[0]
second1 = list1[1]
time1 = float(minite1)*60 + float(second1)
self.lyrics.sort(reverse=True, key=lambda item: float(item.minite) * 60 + float(item.second))
for lyric in self.lyrics:
lyric_time = float(lyric.minite)*60 + float(lyric.second)
if time1 >= lyric_time:
return lyric.words
else:
return '歌曲蓄力中!'
lyric1 = LyricFile()
lyric1.analysis()
lyric1.show_lyrics()
print(lyric1.get_lyric_by_time_stamp('00:00.01'))
print(lyric1.get_lyric_by_time_stamp('02:01.03'))
print(lyric1.get_lyric_by_time_stamp('02:43.03'))
# 蓝莲花
#
# 没有什么能够阻挡
#
# 你对自由地向往
#
# 天马行空的生涯
#
# 你的心了无牵挂
#
# 穿过幽暗地岁月
#
# 也曾感到彷徨
#
# 当你低头地瞬间
#
# 才发觉脚下的路
#
# 心中那自由地世界
#
# 如此的清澈高远
#
# 盛开着永不凋零
#
# 蓝莲花
# 穿过幽暗地岁月
#
# 也曾感到彷徨
#
# 当你低头地瞬间
#
# 才发觉脚下的路
#
# 穿过幽暗地岁月
#
# 也曾感到彷徨
#
# 当你低头地瞬间
#
# 才发觉脚下的路
#
# 心中那自由地世界
#
# 如此的清澈高远
#
# 盛开着永不凋零
#
# 蓝莲花
#歌曲蓄力中!
# 当你低头地瞬间
#
# 盛开着永不凋零
(选做)6.使用面向对象做一个游戏(打飞机和坦克,以及自己设计一个都行)
网友评论