定义Student类
时间: 2023-07-10 14:24:48 浏览: 144
好的,以下是一个简单的定义Student类的示例代码:
```java
public class Student {
// 成员变量
private String name;
private int age;
private String gender;
private String school;
// 构造方法
public Student(String name, int age, String gender, String school) {
this.name = name;
this.age = age;
this.gender = gender;
this.school = school;
}
// 成员方法
public String getName() {
return name;
}
public int getAge() {
return age;
}
public String getGender() {
return gender;
}
public String getSchool() {
return school;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setSchool(String school) {
this.school = school;
}
// 重写toString()方法
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", school='" + school + '\'' +
'}';
}
}
```
在这个示例代码中,我们定义了一个Student类,并且包含了四个成员变量:name、age、gender和school。同时,我们还定义了一个构造方法和四个成员方法,分别用于获取和设置成员变量的值。最后,我们还重写了toString()方法,方便输出Student对象的信息。
阅读全文