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

huangapple go评论78阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月1日 22:43:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/64157734.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定