java语言定义一个Student类,要求: 1、每个学生都有学号stuID(string)和姓名stuName(string) 2、体现字段封装性 3、通过构造方法给每个字段赋值 4、通过showMsg()方法显示字段的值 5、并提供Test类进行测试,要求实例化3个学生对象放到数组中并显示学生的信息
时间: 2023-05-30 20:03:06 浏览: 91
public class Student {
private String stuID;
private String stuName;
public Student(String stuID, String stuName) {
this.stuID = stuID;
this.stuName = stuName;
}
public void showMsg() {
System.out.println("学号:" + stuID + ",姓名:" + stuName);
}
public String getStuID() {
return stuID;
}
public void setStuID(String stuID) {
this.stuID = stuID;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
}
public class Test {
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("001", "张三");
students[1] = new Student("002", "李四");
students[2] = new Student("003", "王五");
for (Student student : students) {
student.showMsg();
}
}
}
阅读全文