C# - 需要基类上的接口,但仅需要派生类中的实现
我正在尝试创建一个基类,为从中派生的许多类提供大量可重用的功能。通过提供此功能,我还希望派生类也实现某些方法,即实现接口。但是,我不想显式告诉派生类它们需要实现该接口,我希望在基类中定义该要求。所以基本上,一旦一个类继承了基类,它就获得了功能,但也需要实现其他方法本身。这就是我想要的,老实说我不确定这是否可能:
public interface IExtraStuff {
bool test();
}
public class BaseControl : System.Web.UI.UserControl, IExtraStuff {
public bool foo(){
return true;
}
// I don't actually want to implement the test() method in this
// class but I want any class that derives from this to implement it.
}
MyUserControl1 : BaseControl {
// this.foo() can be used here
// according to IExtraStuff from the BaseControl, I need to implement test() here
}
基本上我不想将 MyUserControl1
定义为:
MyUserControl1 : BaseControl, IExtraStuff
我希望它在继承额外的接口后自动需要该接口来自BaseControl
的功能。
如果不可能,请告诉我,没关系。我只是对此了解不够。正如我目前编写的那样,它可以编译,并且我觉得我应该得到一个编译错误,因为 test()
没有在 MyUserControl1
中定义。
更新
我已经将我的基类修改为抽象的(我在发布到 SO 之前就已经这样做了),但实际上我可以在不实现抽象方法的情况下构建项目,这就是我开始问这个问题的真正原因问题。下面的代码为我构建,我对此感到困惑:
public interface IExtraStuff {
bool test();
}
public abstract class BaseControl : System.Web.UI.UserControl, IExtraStuff {
public abstract bool test();
public bool foo(){
return true;
}
}
MyUserControl1 : BaseControl {
// this.foo() can be used here
// I can build without implementing test() here!
}
更新 2:问题已解决
事实证明,我的解决方案构建设置中存在问题,并且除非我实现以下抽象方法,否则该项目不会构建基地(现在)。这是我在 Visual Studio 中的错误,而不是类体系结构中的错误。感谢大家的帮助!
I'm trying to create a base class that provides a good amount of re-usable functionality to many classes that will derive from it. By providing this functionality, I'd like to also require the derived classes to implement certain methods as well, i.e. implement an interface. However, I don't want to explicitly tell the derived classes they need to implement the interface, I want that requirement to be defined in the base class. So basically, once a class inherits from the base, it gets functionality, but also the requirement to implement additional methods itself. Here's what I want, and I'm honestly not sure if this is possible:
public interface IExtraStuff {
bool test();
}
public class BaseControl : System.Web.UI.UserControl, IExtraStuff {
public bool foo(){
return true;
}
// I don't actually want to implement the test() method in this
// class but I want any class that derives from this to implement it.
}
MyUserControl1 : BaseControl {
// this.foo() can be used here
// according to IExtraStuff from the BaseControl, I need to implement test() here
}
Basically I don't want to have to define MyUserControl1
as:
MyUserControl1 : BaseControl, IExtraStuff
I want it to automatically require the interface once it inherits the extra functionality from BaseControl
.
If this is not possible, let me know and that's fine. I just don't know enough about it. As I have it currently written, it compiles, and I feel like I should get a compile error since test()
is not defined in MyUserControl1
.
UPDATE
I've modified my base class to be abstract (I had done this prior to posting to SO) but I actually can build the project without implementing the abstract method, that's really why I started to ask this question. The code below builds for me which I'm confused about:
public interface IExtraStuff {
bool test();
}
public abstract class BaseControl : System.Web.UI.UserControl, IExtraStuff {
public abstract bool test();
public bool foo(){
return true;
}
}
MyUserControl1 : BaseControl {
// this.foo() can be used here
// I can build without implementing test() here!
}
UPDATE 2: problem solved
It turns out I had an issue in my solution build setup and the project does not build unless I implement the abstract method from the base (now). This was error on my part in Visual Studio, not an error in the class architecture. Thanks for all the help everyone!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用抽象方法?
请参阅了解更多信息:
http://msdn.microsoft.com/en-us/库/aa664435(VS.71).aspx
编辑
添加派生类实现。
Use abstract methods?
See for more info:
http://msdn.microsoft.com/en-us/library/aa664435(VS.71).aspx
Edit
Adding dervied class impl.
这就是抽象类和方法的用途
,注意,该类也需要标记为抽象
That's what abstract classes and methods are for
Note, the class needs to be marked as abstract also
如果将 BaseControl 设为抽象类,则可以省略接口成员,从而强制在子类中实现。
If you make BaseControl an abstract class, you can omit the interface members, and so, force the implementation in child classes.