'''設計乙個學生類:屬性:姓名、學號、年齡、成績,'''
'''設計乙個班級類:屬性:班級代號、所有學生'''
'''要求:實現向班級新增學生、刪除學生、學生排序(指定條件)、查詢學生資訊(姓名、學號等)'''
class student:
def __init__(self, name, school_num, age, score):
# 定義屬性姓名、學號、年齡、成績
self.name = name
self.school_num = school_num
self.age = age
self.score = score
self.student =
class class:
def __init__(self, class_num, students):
# 初始化 班級屬性
self.class_num = class_num
self.students = students
# print(self.students)
def add_student(self, student):
# 將student類裡的資訊以列表的形式儲存到class類裡面
# print(self.students[0])
def del_student(self, student):
# 便利列表找到匹配的學生然後刪除
"""for i in range(len(self.students)):
if self.students[i] == student.student:
del self.students[i]
"""# 或者使用remove直接從列表中刪除
self.students.remove(student.student)
def sort_stu(self, key):
# 排序
self.students.sort(key=lambda a: a[key])
def show(self):
for i in range(len(self.students)):
print(self.students[i])
def query(self, key, value):
for i in range(len(self.students)):
if self.students[i][key] == value:
print(self.students[i])
s1 = student("n1", 1, 13, 89)
s2 = student("n2", 2, 14, 90)
c1 = class("c1", )
c1.add_student(s1)
c1.show()
c1.del_student(s1)
c1.show()
c1.add_student(s2)
c1.add_student(s1)
c1.sort_stu("score")
c1.query("school_num", 1)
方法二:
class student:
def __init__(self, name, num, age, score):
self.name = name
self.num = num
self.age = age
self.score = score
def __str__(self):
return 'name:{},num:{},age:{},score:{}'.format(self.name, self.num, self.age, self.score)
# 班級類
class class:
def __init__(self, name):
self.name = name
self.stu_list =
self.stu_dict = {}
# 新增學生
def add_stu(self, stu):
self.stu_dict[stu.name] = stu
# 刪除學生
def del_stu(self, stu):
self.stu_list.remove(stu)
self.stu_dict.pop(stu.name)
# 查詢學生資訊
def search_by_name(self, name):
return self.stu_dict.get(name)
# 排序
def sort_by(self, key=none, reverse=false):
self.stu_list.sort(key=key, reverse=reverse)
# 展示學生資訊
def show_stu(self):
for s in self.stu_list:
print(s)
from random import randint, uniform
# 建立班級
c = class('zz-jy-py1804')
for i in range(10):
name = 'stu' + str(i+1)
num = i+100
age = randint(20, 30)
score = uniform(0, 100)
# 建立學生
s = student(name, num, age, score)
# 新增學生
c.add_stu(s)
# 排序
c.sort_by(key=lambda s: s.score, reverse=true)
c.show_stu()
結果:name:stu3,num:102,age:22,score:95.35498927460175
name:stu1,num:100,age:27,score:86.71827270053991
name:stu7,num:106,age:20,score:82.57020984238078
name:stu6,num:105,age:20,score:74.8175599855444
name:stu5,num:104,age:27,score:68.95524475284302
name:stu4,num:103,age:23,score:62.47905815211614
name:stu10,num:109,age:29,score:53.41054734341052
name:stu8,num:107,age:28,score:43.26452706210031
name:stu9,num:108,age:23,score:36.46406569534819
name:stu2,num:101,age:30,score:16.36399277241821
Python中的類學習筆記
python使用中物件導向的語言,支援繼承 多型 定義乙個person類 複製 如下 class person def sayhello self print hello person.sayhello none hello person sayhello hello 可以修改person的類方法 ...
python中類的特點 Python中的類(一)
python中的類 一 一 應用場景 如果多個函式中有一些相同的引數時,轉換成物件導向。二 如何建立類 類是用來描述具有相同的屬性和方法的物件的集合。它定義了該集合中每個物件所共有的屬性和方法。物件是類的例項。class 類名 pass 三 類變數 類變數在整個例項化的物件中是公用的。類變數定義在類...
python 類的學習
關於類的概念等,自己已經略微了解了一些,類的介紹,就不贅述了。結合python的化 最基礎的使用 有幾個點 1 建立類的方法 類命名規則 駝峰 class mybook 如有繼承 類名後加括號,括號裡寫父類 class mybook book 以冒號結束 2 類中的變數 屬性 類中的函式 方法 2....