我初学java能说的详细点吗?
谢谢

解决方案 »

  1.   

    public class Myclass{
     private int x;
     private int y;
     public myclass(int x, int y){
      this.x = x;
      this.y = y;
     }
     public myclass(){}
                      
     public void setPara(int x, int y){
      this.x = x;
      this.y = y;
      System.out.println("x="+x+"  y="+y);
     } public static void main(String args[]){
      myclass my[] =new myclass[20];
      for(int i=0;i<=19;i++) 
       {
         my[i] = new myclass(1,1);
       }
      for(int i=0;i<=19;i++){
       my[i].setPara(10,15);
      }
     }
    }我帮你改了一下,我觉得你写的程序很不规范啊。
      

  2.   

    楼主理解错了对象数组的含义。myclass[] new = myclass[20];他只是创建了一个数组,而没有创建每个元素对应的对象。故此应该是这样写的。
    myclass[] new = myclass[20];
      for(i=0;i<=20;i++){
       new[i]=new myclass(x,y);
      }
      

  3.   

    呵呵,没有注意他的i<20也写错了,hoho
      

  4.   

    你写的方法,有一个错误,setPara应该写返回类型
    public void setPara(int x, int y){
      this.x = x;
      this.y = y;
    }public static void main(String args[]){
      myclass my[] =new myclass[20];
      for(int i=0;i<=19;i++) 
       {
         my[i] = new myclass(1,1);
       }
      for(int i=0;i<=19;i++){
       my[i].setPara(10,15); //已经在调用方法了。
      }
     }
      

  5.   

    (int i=0 ;i<20 ;i++)是没错的 
    如果要写(int i=0;i<=20;i++)会out,最大下标是19
    要就按我头上的写,(int i =0;i<=19;i++) 
    但编译时会检查数组边界,问题倒不大。