简体   繁体   中英

Properly Implement Equals and GetHashCode in Sub Class

Let's assume I have the following Abstract Class Student:

public abstract class Student
{
    public string studentID {get; private set;}
    public string FirstName {get; private set;}
    public string lastname {get; private set;}
    public int age {get; private set;}

    public Student(string id,string firstname, string lastname, int age)
    {
        this.FirstName = firstname;
        this.lastname = lastname;
        this.age = age;
    }

    public abstract void calculatePayment();

}

and the following sub-class:

public class InternationalStudent:Student
{
    public bool inviteOrientation {get; private set;}

    public InternationalStudent(string interID,string first, string last, int age, bool inviteOrientation)
        :base(interID,first,last,age)
    {
        this.inviteOrientation = inviteOrientation;

    }

    public override void calculatePayment()
    {
       //implementation left out
    }

}

Main

InternationalStudent svetlana = new InternationalStudent("Inter-100""Svetlana","Rosemond",22,true);

InternationalStudent checkEq = new InternationalStudent("Inter-101","Trisha","Rosemond",22,true);

Question:

How would I properly implement my equalsTo method inside my subclass? I've been reading here but I'm confused because some answers indicate that a subclass shouldn't know the members of the parent class.

If I wanted to implement IEquality in a subclass, so that svetlana.Equals(checkEq); returns false, how would I go it?

As per the comments what makes the object equals is if the ID, First name, and Last name are the same.

I'm confused because some answers indicate that a subclass shouldn't know the members of the parent class.

You have that backwards. A subclass has complete knowledge of all non-private parent class members.

How would I properly implement my equalsTo method inside my subclass?

What defines "equality" in your subclass? Typically it's a combination of property values, but most of the pertinent properties are on the parent class, so would two "students" with the same first name, last name, age be "equal"?

public override bool Equals(object obj)
{
    var student = obj as InternationalStudent;

    if (student == null)
    {
        return false;
    } 

    return this.FirstName == student.FirstName &&
           this.lastname  == student.lastname &&
           this.age       == student.age;
}

However, since all of the [properties in question are inherited, perhaps this belongs on the parent class instead of the sub class.

Define Equals in base class, then call it from the child class

public override bool Equals(object obj) {
    var other = obs as InternationalStudent;
    return other!= null && base.Equals(obj) && other.inviteOrientation == this.inviteOrientation;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM