【发布时间】:2017-06-13 09:51:27
【问题描述】:
因此,学校已将我们转移到 Java,而没有真正解释基础知识。
现在我有一个问题,我有一个带有方法 equalsTest 的类,用于测试两个名称是否相同。然后在我的主要方法中,如果名称相同(equalsTest = true),我必须调用equalsTest的结果并打印“相同的朋友”,如果名称不同(equalsTest = false)则打印“不同的朋友”
public class Person {
/* Attribute declarations */
private String lastName; // last name
private String firstName; // first name
private String email; // email address
public Person(String firstName, String lastName, String email) {
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
//there are a few other methods in here but are not necessary for the issue explained
public boolean equalsTest(Person other){
if (this.firstName.equals(other.firstName)&& this.lastName.equals(other.lastName))
return true;
else
return false;
public static void main (String[] args) {
// create a friend
Person friend1 = new Person("Mickey", "Mouse", "");
friend1.setEmail("mickey@uwo.ca");
// create another friend
Person friend3 = new Person ("Mickey", "Mouse", "");
// create a friend without email
Person friend2 = new Person("Minnie", "Mouse", "");
//Here I need another method that calls the "true" or "false" boolean result of the equalsTest method above and says "if equalsTest true, print "Same Friend", and if equalsTest false, print "Not Same Friend", etc.
另外,实验室的部分要求是我不能编辑 equalsTest 方法,我必须在 main 方法中调用它。我知道只在主要方法中创建一个分析它的部分会更容易,但不能这样做。
【问题讨论】:
-
您在寻找什么?
friend2.equalsTest(friend3)? -
正如解释的那样,我有一个方法,如果friend1等于friend2,则返回true或false,我需要的是另一种方法中的一段代码,以调用第一种方法的结果,如果结果为真,打印出“同一个朋友”,如果结果为假,则打印出“不是同一个朋友”
标签: java eclipse if-statement methods boolean