强制子类通过父级覆盖祖先的功能 [英] Force child class to override function of ancestor via parent

查看:81
本文介绍了强制子类通过父级覆盖祖先的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一种算法,要求用户创建自己的类,该类继承自我定义的类。但是,该算法要求用户覆盖C#标准库中的Equals和GetHashCode函数。



我可以强制从我的类继承的类来实现GetHashCode和Equals函数?

  public abstract int GetHashCode(); 

在我的基类中写这个不是一个选项,因为我的基类从它的父级继承了GetHashCode,它已经实现的地方。

解决方案

这就是你要找的东西。由于你的类是抽象的,你可以毫无问题地做到这一点。

  public abstract override int GetHashCode(); 

尽管它派生自其他一些类,但这使得你的子类必须覆盖这个方法。 / p>

I am writing an algorithm which requires the user to create his own class which inherits from a class defined by me. However, the algorithm requires the user to override the Equals and GetHashCode functions from the C# standard libraries.

Can I force the class inherited from my class to implement the GetHashCode and Equals functions?

public abstract int GetHashCode();

Writing this in my base class is not an option, as my base class inherits GetHashCode from it's parent, where it is implemented already.

解决方案

This is what you're looking for. Since your class is abstract you can pretty much do this without any problem.

public abstract override int GetHashCode();

This despite of it derived from some other class, this makes your sub class must override this method.

这篇关于强制子类通过父级覆盖祖先的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆