python编程定义一个学生类(Player),属性有姓名、学号、学院、专业、班级。并实例化2个学生,分别是张三、李四和王五
时间: 2023-10-20 13:26:03 浏览: 87
可以按照以下方式定义一个学生类(Player):
```python
class Player:
def __init__(self, name, student_id, college, major, class_name):
self.name = name
self.student_id = student_id
self.college = college
self.major = major
self.class_name = class_name
```
然后,我们可以实例化两个学生,如下所示:
```python
zhangsan = Player("张三", "001", "计算机学院", "计算机科学与技术", "1班")
lisi = Player("李四", "002", "计算机学院", "软件工程", "2班")
wangwu = Player("王五", "003", "信息学院", "信息管理与信息系统", "3班")
```
这里我们不仅实例化了张三和李四,还实例化了一个王五。这样,我们就可以通过调用它们的属性来访问它们的信息,例如:
```python
print(zhangsan.name)
print(lisi.major)
print(wangwu.class_name)
```
输出:
```
张三
软件工程
3班
```
阅读全文