简体   繁体   English

在子类中正确实现Equals和GetHashCode

[英]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? 如何在子类中正确实现equalsTo方法? 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); 如果我想在子类中实现IEquality,那么svetlana.Equals(checkEq); returns false, how would I go it? 返回false,我该怎么办?

As per the comments what makes the object equals is if the ID, First name, and Last name are the same. 根据注释,使对象相等的是ID,名和姓是否相同。

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? 如何在子类中正确实现equalsTo方法?

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 在基类中定义Equals,然后从子类中调用它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为IRevertibleChangeTracking类实现GetHashCode - Implement GetHashCode for IRevertibleChangeTracking class 如何使用覆盖的逻辑Equals()实现GetHashCode()的覆盖 - how to implement override of GetHashCode() with logic of overriden Equals() 为什么GetHashCode实现与Equals相同的逻辑? - Why should GetHashCode implement the same logic as Equals? 如果我的类实现了IEqualityComparer,我应该实现非通用GetHashCode和Equals吗? <T> ? - Should I implement non-generic GetHashCode and Equals if my class implements IEqualityComparer<T>? class在Castle.Core中实现了IProxyGenerationHook,如何实现Equals和GetHashCode的override方法? - How to implement the override methods of Equals and GetHashCode in a class that implements IProxyGenerationHook in Castle.Core? 在结构中,通过Equals实现operator ==是否有效,但不能覆盖Equals和GetHashCode? - In a struct, is it valid to implement operator== via Equals, but not override Equals and GetHashCode? 没有状态的派生类中的 Equals/GetHashCode 覆盖警告 - Equals/GetHashCode override warning in derived class with no state GetHashCode等于C#中的类的实现 - GetHashCode Equals implementation for a class in C# 覆盖Equals和GetHashCode-派生类中的默认实现 - Overriding Equals and GetHashCode - default implementation in derived class 用一个字段覆盖 class 中的 Equals 和 GetHashCode - Override Equals and GetHashCode in class with one field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM