强制接口的子类实现ToString [英] Force subclasses of an interface to implement ToString
本文介绍了强制接口的子类实现ToString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
说我有一个接口IFoo
,并且我希望IFoo
的所有子类都覆盖Object的ToString
方法.这可能吗?
Say I have an interface IFoo
and I want all subclasses of IFoo
to override Object's ToString
method. Is this possible?
仅将方法签名添加到IFoo中是不起作用的:
Simply adding the method signature to IFoo as such doesn't work:
interface IFoo
{
String ToString();
}
因为所有子类都扩展了Object
并以这种方式提供了实现,所以编译器不会抱怨它.有什么建议吗?
since all the subclasses extend Object
and provide an implementation that way, so the compiler doesn't complain about it. Any suggestions?
推荐答案
我不认为您可以使用界面来实现.但是,您可以使用抽象基类:
I don't believe you can do it with an interface. You can use an abstract base class though:
public abstract class Base
{
public abstract override string ToString();
}
这篇关于强制接口的子类实现ToString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文