英文:
Im trying to create some plant objects to display on my test class in Java but the first part of the result is displaying all 3 variables I put
问题
以下是您提供的代码的翻译部分:
public class Plant{
private String name;
private int height; // 当前高度
private boolean edible; // 是否可食用
public Plant(){
name = "无名植物";
height = 0;
edible = false;
}
public Plant(String newName){
name = newName;
height = 0;
edible = false;
}
public Plant(String newName, int newHeight, boolean newEdible){
name = newName;
height = newHeight;
edible = newEdible;
}
public String getName(){
return name;
}
public int getHeight(){
return height;
}
public boolean getEdible(){
return edible;
}
public void setName(String name){
this.name = name;
}
public void setHeight(int height){
this.height = height;
}
public void setEdible(boolean edible){
this.edible = edible;
}
public String toString(){
return name + "。高度为:" + height + "。可食用吗?" + edible ;
}
}
public class TestClass {
public static void main(String[] args) {
Plant rose = new Plant("玫瑰", 5, false);
Plant orchids = new Plant("兰花", 4, false);
Plant carrot = new Plant("胡萝卜", 5, true);
System.out.println(rose);
System.out.println(orchids);
System.out.println(carrot);
}
}
第二部分和第三部分的输出是正确的,但第一部分的输出不符合预期,它显示了所有三个变量,而不仅仅是第一个变量。
英文:
So I'm new to java and was trying to create an object in my test class using certain variables I give it. Here is the plant code I did so far.
public class Plant{
private String name;
private int height; // current height
private boolean edible; // is it edible or not
public Plant(){
name = "No Name Plant";
height = '0';
edible = false;
}
public Plant(String newName){
name = newName;
height = '0';
edible = false;
}
public Plant(String newName, int newHeight, boolean newEdible){
name = newName;
height = newHeight;
edible = newEdible;
}
public String getName(){
return name;
}
public int getHeight(){
return height;
}
public boolean getEdible(){
return edible;
}
// public int getHeight(){
// return height;
// }
// public boolean getEdible(){
// return edible;
// }
public void setName(String name){
this.name = name;
}
public void setHeight(int height){
this.height = height;
}
public void setEdible(boolean edible){
this.edible = edible;
}
public String toString(){
return name + ". Height is " + height + ". Is it edible? " + edible ;
}
}
And here is the test class I set up to create 3 objects.
public class TestClass {
public static void main(String[] args) {
Plant rose = new Plant("Rose" + 5 + false);
Plant orchids = new Plant("Orchids" + 4 + false);
Plant carrot = new Plant("Carrot" + 5 + true);
System.out.println(rose);
System.out.println(orchids);
System.out.println(carrot);
}
}
The second and third part come out fine but the first part doesn't come out the way I want it to, it shows all 3 variables at once instead of just the first one.
答案1
得分: 2
在类TestClass
的方法main()
中,您调用类Plant
的构造函数时,请将+
替换为逗号,即:
Plant rose = new Plant("Rose", 5, false);
Plant orchids = new Plant("Orchids", 4, false);
Plant carrot = new Plant("Carrot", 5, true);
+
是字符串连接运算符,逗号用于在调用构造函数(或任何类型的方法)时分隔参数。
另请注意,如果删除类Plant
中的前两个构造函数,则类TestClass
将无法编译。
英文:
In method main()
of class TestClass
, where you call the constructor for class Plant
, replace the +
with a comma, i.e.
Plant rose = new Plant("Rose" , 5 , false);
Plant orchids = new Plant("Orchids" , 4 , false);
Plant carrot = new Plant("Carrot" , 5 , true);
The +
is the string concatenation operator and comma is used to separate arguments when calling a constructor (or any kind of method).
Also note that if you remove the first two constructors in class Plant
then class TestClass
will not compile.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论