Java中的对象数组

Java中的对象数组

对象数组的声明
类 数组名称[]=new 类[数组大小]
或者
类 数组名称[]={括号里面填内容}
实例
Perp pp[] = new Perp[3];
Perp pip[]={new Perp(12),new Perp(13)};
使用匿名对象数组
print(new Perp[]{new Perp(12),new Perp(14),new Perp(1345)});
上面的那行代码中的print是定义的一个方法,具体如下

public static void print(Perp pr[]){
        for(int i=0;i<pr.length;i++){
            System.out.println(i+"=="+pr[i]);
        }
    }

Perp是定义的一个类,具体如下

class Perp{
    public Perp(int age){
        this.setAge(age);
    }
    private int age;
    public void setAge(int age){
        this.age=age;
    }
    public int getAge(){
        return this.age;
    }
}
posted @ 2020-01-15 10:37  sogeisetsu  阅读(696)  评论(0编辑  收藏  举报