比较构造函数对象Java

时间:2016-07-26 21:19:49

标签: java compare

多么棒的平台!我希望我不要问一个太愚蠢的问题,但我没有成功地寻找答案。

问: 是否可以比较构造函数创建的对象值?就像我想让动物战斗并比较" str"对彼此的价值观。 我的目标是创造"战斗" Animal类中的方法,而不是main。通过这种方式,我可以称之为" dog.fight();

请参阅我的代码示例(对不起我的英文)

public class Animal {

private int str;
private int agi;
private String name;
private String eyeColour;


public void set (int strenght, int agility, String _name){
    str = strenght;
    agi = agility;
    name = _name;

}
public String get (){
    System.out.println("Created a new animal named " + name +"! ");
    System.out.println(name + "'s agility is " + agi);
    System.out.println(name + "'s strenght is " + str);

    return name + str + agi;
}       

}

import java.util.Scanner;

public class Hello {

public static void main(String[] args) {

    Animal dog = new Animal ();
    dog.set(8, 4, "Rambo");
    dog.get();

    System.out.println("");
    Animal cat = new Animal ();
    cat.set(2, 9, "Felix");
    cat.get();


}
}

1 个答案:

答案 0 :(得分:1)

你需要创建一种方法'对抗'在Animal类中,将一个Animal对象作为参数,并使用它返回获胜者的结果。

以下是代码:

public class Animal  {

    private int str;
    private int agi;
    private String name;
    private String eyeColour;


    public void set (int strenght, int agility, String _name){
        str = strenght;
        agi = agility;
        name = _name;

    }
    public String get (){
        System.out.println("Created a new animal named " + name +"! ");
        System.out.println(name + "'s agility is " + agi);
        System.out.println(name + "'s strenght is " + str);

        return name + str + agi;
    }

    public String fight(Animal rival){
        //Provide Comparison Logic Here
        if(this.str>rival.str)return this.name;
        if(this.str < rival.str)return rival.name;
        return "No One ";
    }
}


public class Hello {

    public static void main(String[] args) {

        Animal dog = new Animal ();
        dog.set(8, 4, "Rambo");
        dog.get();

        System.out.println("");
        Animal cat = new Animal ();
        cat.set(2, 9, "Felix");
        cat.get();

        System.out.println(dog.fight(cat)+" is the winner! ");

    }
}

小方注意到:

在这里使用构造函数而不是set方法会好得多,因为通常会创建setter和getter来设置或获取单个变量。

最好更改get方法的名称并覆盖toString方法。

以下是修改过的代码:

public class Animal  {

    private int str;
    private int agi;
    private String name;
    private String eyeColour;


    public Animal (int str, int agi, String name){
        this.str = str;
        this.agi = agi;
        this.name = name;
    }

    @Override
    public String toString(){
        String description = "Created a new animal named " + name +"!\n";
        description+=name + "'s agility is " + agi+"\n";
        description+=name + "'s strenght is " + str;
        return description;
    }

    public String fight(Animal rival){
        //Provide Comparison Logic Here
        if(this.str>rival.str)return this.name;
        if(this.str < rival.str)return rival.name;
        return "No One ";
    }
}


public class Hello {

    public static void main(String[] args) {

        Animal dog = new Animal (8, 4, "Rambo");
        System.out.println(dog);

        System.out.println();
        Animal cat = new Animal (2, 9, "Felix");
        System.out.println(cat);

        System.out.println(dog.fight(cat)+" is the winner! ");

    }
}