0
package wr3;

public class Person {
    private String name;
    private String id;
    private String bday;
    private String address;

    public String getName(){
        return name;
    }

    public String getID(){
        return id;
    }

    public String getBday(){
        return bday;
    }

    public String getAdd(){
        return address;
    }


    public void equals(){
        super.equals(id);
    }
    @Override
    public String toString(){
        return(name + bday + id + address);
    }
}

package wr3;

public class Test {
    public static void main(String[] args){
        String name = "Claude Rhay Torre";
        String name2 = "Bea Señerpida";
        String id = "10302993";
        String id2 = "11102825";   
        String bday = "06/201993";
        String bday2 = "11/17/1994";
        String address = "BF Better Living Basak LLC";
        String address2 = "F Martyr St Poblacion LLC";
        boolean eq;

        System.out.println(name.toString());
        System.out.println(id.toString());
        System.out.println(bday.toString());
        System.out.println(address.toString());
        System.out.println();

        System.out.println(name2.toString());
        System.out.println(id2.toString());
        System.out.println(bday2.toString());
        System.out.println(address2.toString());


        eq = id.equals(id2);
        System.out.println("\nDo they have the same ID number? " + eq );

    }
}

So I have this code. And I also have this problem.

  • A. Object class
    1. Study the Object class in the java.lang package.
    2. Understand all its methods.
    3. Create a Person class with the requirements:
      a. Implement encapsulation
      b. The fields are: name, ID (identification number), birthday, and address.
      c. A method that will override the equals( ) method of Object class. Two persons are equal if they have the same id.
      d. A method that will override the toString( ) method of Object class. It displays the id, name, birthday, and address of a Person object.
    4. Create a test class to create Person objects and call the equals( ) and toString( ) methods appropriately.

My question is, are these two classes even related? What I mean, is my "toString" and "equal" method called on my Test class the one that is in my Person's class? Or is it the "toString" and "equal" methods on the Object class?

How can I override the equals and toString class in the Object class?

4

1 回答 1

0

当从另一个类(例如public String toString()from Object)继承一个方法时,您所要做的就是public String toString()在您的类中定义您自己的方法来覆盖它。您实际上已经在您的代码中做到了这一点,并且您已经成功地覆盖toString()了所有Person.

您没有看到预期结果的原因是您调用的toString()equals()方法来自String类,而不是来自您的Person类。这是因为您是在String对象而不是Person对象上调用它们 - 并且这两个类在层次上不相关(除了它们的共同Object祖先)。

Test可能是这样的,而不是:

    Person p1 = new Person();
    p1.name = "Claude Rhay Torre";
    p1.id = "10302993";
    p1.bday = "06/20/1993";
    p1.address = "BF Better Living Basak LLC";

    Person p2 = new Person();
    p2.name2 = "Bea Señerpida";
    p2.id2 = "11102825";   
    p2.bday2 = "11/17/1994";
    p2.address2 = "F Martyr St Poblacion LLC";

    System.out.println(p1.toString());
    System.out.println(p2.toString());
于 2013-08-06T21:39:21.240 回答