用java编写,要求如下:(1)设计一个Student类包含属性班级banji和姓名name;有构造方法Student(String banji,String name),有toString方法显示该生信息; (2)创建一个Student类的数组,输入3个同学的信息。调用toString遍历输出数组中全部同学的信息。
时间: 2023-06-10 22:04:03 浏览: 92
好的,我明白了。以下是您所需的Java代码:
public class Student {
private String banji;
private String name;
public Student(String banji, String name) {
this.banji = banji;
this.name = name;
}
@Override
public String toString() {
return "姓名:" + name + ",班级:" + banji;
}
public static void main(String[] args) {
Student[] students = new Student[3];
students[0] = new Student("三年一班", "张三");
students[1] = new Student("三年一班", "李四");
students[2] = new Student("三年二班", "王五");
for (Student student : students) {
System.out.println(student.toString());
}
}
}
运行结果为:
姓名:张三,班级:三年一班
姓名:李四,班级:三年一班
姓名:王五,班级:三年二班
阅读全文