/** * 创建一个学生类,有四个属性(姓名、学号、年级、性别) * 给每个属性赋予getter、setter方法 * 学生类有学习、考试、打游戏的方法(方法输出随便写) * 创建两个学生类实例化对象,第一个调用无参构造方法,然后通过getter\setter方法给赋予属性值 * 第二个,调用全参构造方法,给每一个属性都初始化。 * */
时间: 2024-02-17 22:11:29 浏览: 42
好的,下面是实现代码:
```
public class Student {
private String name;
private String number;
private String grade;
private String gender;
public Student() {}
public Student(String name, String number, String grade, String gender) {
this.name = name;
this.number = number;
this.grade = grade;
this.gender = gender;
}
// getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
// study, exam, and play methods
public void study() {
System.out.println(name + " is studying.");
}
public void exam() {
System.out.println(name + " is taking an exam.");
}
public void play() {
System.out.println(name + " is playing a game.");
}
}
// main method for testing
public static void main(String[] args) {
Student stu1 = new Student();
stu1.setName("Tom");
stu1.setNumber("001");
stu1.setGrade("Grade 10");
stu1.setGender("Male");
Student stu2 = new Student("Lisa", "002", "Grade 11", "Female");
stu1.study();
stu1.exam();
stu1.play();
stu2.study();
stu2.exam();
stu2.play();
}
```
希望能够帮助到你。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)