具有来自超类对象

时间:2017-10-24 17:41:08

标签: java object inheritance subclass superclass

例如:我认为ant1有自己的“路径”数组,而ant2有不同的“路径”数组。是否有可能ant1和ant2都有一个(和相同的)“path”数组属于“蚁丘”?

        class Anthill{
            protected int MAX_X=15;
            protected int MAX_Y=15;
            protected int[][] path=new int[MAX_X][MAX_Y];
            public void print(){
            for(int i=0;i<MAX_X;i++){
                    for(int j=0;j<MAX_Y;j++){
                            if(path[i][j]==0){
                                    System.out.print(" ");
                            }
                            else if(path[i][j]==1)
                                    System.out.print("x");
                            else
                                    System.out.print(".");
                    }
                    System.out.println();
            }
            }

            public static void main(String arg[]){
                    Anthill anthill=new Anthill ();
                    Ant ant1=anthill.new Ant();
                    Ant ant2=anthill.new Ant();
            }

            class Ant extends Anthill{
                    int x=0;
                    int y=0;



                    Ant(){
                            Random ranGen=new Random();
                            x=ranGen.nextInt(MAX_X);
                            y=ranGen.nextInt(MAX_Y);
                            super.path[x][y]=1;
                    }

    }
}

0 个答案:

没有答案