用JAVA创建一个班级类
时间: 2024-05-12 08:17:48 浏览: 17
好的,很高兴为您解答。以下是一个用JAVA创建班级类的示例代码:
```
public class Class {
private String className;
private int classCapacity;
private int numOfStudents;
public Class(String name, int capacity) {
this.className = name;
this.classCapacity = capacity;
this.numOfStudents = 0;
}
public String getClassName() {
return className;
}
public int getClassCapacity() {
return classCapacity;
}
public int getNumOfStudents() {
return numOfStudents;
}
public void addStudent() {
if (numOfStudents < classCapacity) {
numOfStudents++;
System.out.println("A student has been added to " + className);
} else {
System.out.println("Sorry, the class is full.");
}
}
public void removeStudent() {
if (numOfStudents > 0) {
numOfStudents--;
System.out.println("A student has been removed from " + className);
} else {
System.out.println("There is no student in the class.");
}
}
}
```
这个例子创建了一个班级类,包括班级名称、班级容量和学生数量等属性,以及添加学生和删除学生的方法。您可以根据需要进一步修改和完善。