编写一个Java程序,定义一个表示学生的类,类名Student, 其成员变量有:学号、班级姓名、性别、年龄; 成员方法:(1)获得班号(2)获得姓名(3)获得年龄(4)修改年龄; 创建一个Student的对象,修改该对象的年龄,并输出该对象的年龄
时间: 2024-05-25 12:16:55 浏览: 24
public class Student {
private String studentNo; //学号
private String className; //班级姓名
private String gender; //性别
private int age; //年龄
public Student(String studentNo, String className, String gender, int age) {
this.studentNo = studentNo;
this.className = className;
this.gender = gender;
this.age = age;
}
public String getClassName() {
return className;
}
public String getGender() {
return gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Main {
public static void main(String[] args) {
Student student = new Student("1001", "Class1", "Male", 18);
student.setAge(19);
System.out.println("Student's age is " + student.getAge());
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)