继续得到“null has 00null”

时间:2013-11-28 18:07:15

标签: java class

我正在使用独立课程和主要驱动程序,这是独立课程:

public class Bugs{
   private String bugType;
   private int legs;
   private int arms;
   private String nativeTo;

   public Bugs(String bt, int l, int a, String nt){
      bt=bugType;
      l=legs;
      a=arms;
      nt=nativeTo;
   }

   public Bugs(String bt, int l, int a){
      bt=bugType;
      l=legs;
      a=arms;
      nativeTo="Not known";
   }

   public String getbt(){
      return bugType;
   }

   public void setbugType(String bugType){
      this.bugType=bugType;
   }

   public int getlegs(){
      return legs;
   }

   public void setlegs(int legs){
      this.legs=legs;
   }

   public int getarms(){
      return arms;
   }

   public void setarms(int arms){
      this.arms=arms;
   }

   public String getnativeTo(){
      return nativeTo;
   }

   public void setnativeTo(String nativeTo){
      this.nativeTo=nativeTo;
   }

   public String toString(){
      return bugType + " has " + legs + arms + nativeTo;
   }
}

这是主要的驱动因素:

public class myBugs{
   public static void main (String args[]){
      Bugs asiaBeetle = new Bugs("Asian Beetle", 2, 2, "Japan");
      Bugs spider = new Bugs("Spider", 1000, 0);
      Bugs americanBeetle = new Bugs("American Beetle", 2, 2, "USA");
      System.out.println(asiaBeetle);
   }
}

每次运行主驱动程序时,JGRASP都会一直返回“null has 00null”。我做错了什么?

2 个答案:

答案 0 :(得分:4)

在构造函数中交换您的作业:

public Bugs(String bt, int l, int a, String nt){
      bugType = bt;
      legs = l;
      arms = a;
      nativeTo = nt;
}

你必须为另一个做同样的事情:

public Bugs(String bt, int l, int a){
    bugType = bt;
    legs = l;
    arms = a;
    nativeTo="Not known";
}

答案 1 :(得分:2)

将你的构造函数改为这个offcourse为另一个做同样的事情

public Bugs(String bt, int l, int a, String nt){
      bugType=bt;
      legs=l;
      arms=a;
      nativeTo= nt;
   }