学生类:
class A{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
//学生类使用方法:
public class test(){
public static void main(String[] args) {
A[] students = new A[2]; //创建2个学生的学生数组
A as= new A();
as.setAge(15);
as.setName("tom");
A as1= new A();
as1.setAge(16);
as1.setName("cat");
A[] a={as,as1}; //动态创建学生数组
for (A st : a) {//遍历数组
System.out.println(st.getName()+";");
}
}
}
运行结果:
tom;
cat;