gpt4 book ai didi

java - 继续获取 "null has 00null"

转载 作者:行者123 更新时间:2023-12-01 08:07:33 26 4
gpt4 key购买 nike

我正在使用一个独立的类和一个主要驱动程序,这是独立的类:

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”。我做错了什么?

最佳答案

在构造函数中交换分配:

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";
}

关于java - 继续获取 "null has 00null",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com