學生類:
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;
學生類:
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;