以下是一个使用Python实现班级姓名管理的示例代码:

class Student:
    def __init__(self, name):
        self.name = name
        
class Class:
    def __init__(self):
        self.students = []
        
    def add_student(self, name):
        student = Student(name)
        self.students.append(student)
        
    def remove_student(self, name):
        for student in self.students:
            if student.name == name:
                self.students.remove(student)
                
    def get_student_list(self):
        student_list = []
        for student in self.students:
            student_list.append(student.name)
        return student_list
    
    def get_num_students(self):
        return len(self.students)

使用示例:

# 创建一个班级对象
my_class = Class()

# 添加学生
my_class.add_student("Alice")
my_class.add_student("Bob")
my_class.add_student("Charlie")

# 获取学生列表
print(my_class.get_student_list()) # ['Alice', 'Bob', 'Charlie']

# 删除学生
my_class.remove_student("Bob")

# 获取学生数量
print(my_class.get_num_students()) # 2
用Python实现班级姓名的管理

原文地址: https://www.cveoy.top/t/topic/byxj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录