gpt4 book ai didi

java - Java 比较构造函数对象

转载 作者:行者123 更新时间:2023-12-01 09:38:57 25 4
gpt4 key购买 nike

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

问:是否可以比较构造函数创建的对象值?就像我想让动物打斗并相互比较“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();


}
}

最佳答案

您需要在 Animal 类中创建一个“fight”方法,该方法将 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! ");

}
}

关于java - Java 比较构造函数对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38600053/

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