实现后密封接口
我正在做一个小项目,我遇到了这个问题。
项目输出是一个包含接口的库。如果可能的话,我想实现该接口并密封其中的函数:
public interface ITest
{
void SomeMethod();
}
class A : ITest
{
public sealed override SomeMethod()
{
}
}
这个想法是让每个人都可以使用该接口,并有一些专门的类来实现它。例外的是,我想确保如果有人创建 A 类型的专门类,他/她将无法更改该方法的行为。
问题是您不能将“override”关键字放在那里,因为该方法没有在接口中声明为“虚拟”。而且您不能在界面中将其声明为“虚拟”,因为这是不允许的。并且您无法删除“override”关键字,因为“sealed”需要它。
任何解决方法或集思广益的想法都会受到欢迎,但如果有人能想出一个包含界面的解决方案,我会很高兴学习它!
谢谢!
编辑:忘记这个问题!就像 Ani 所说,我忘记了 C# 中的默认方法是密封的。似乎偶尔回到基础知识总是好的......
I am working on a small project and I came across that problem.
The project output is a library containing an interface. I would like to implement that interface and seal the functions in it like this if possible:
public interface ITest
{
void SomeMethod();
}
class A : ITest
{
public sealed override SomeMethod()
{
}
}
The idea is to have the interface available to everyone and have some specialized class that implements it. The exception is that I want to make sure that if someone create a specialized class of type A, he/she won't be able to change the method's behavior.
The problem is you can't put the "override" keyword in there since the method isn't declared as "virtual" in the interface. And you can't declare it as "virtual" in the interface since it's not allowed. And you can't remove the "override" keyword since it's needed by "sealed".
Any workaround or brainstorming idea would be welcome, but if someone can come up with a solution that includes an interface, I'd be really happy to learn it!
Thanks!
EDIT: Forget this question! Like Ani said, I forgot that by default method in C# are sealed. Seems like it's always good to go back to the basics once in a while...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我可能完全误解了这个问题,但如果您的意图是将方法密封在
A
中,您可以这样做:与 Java 不同,C# 中的方法默认是密封的。
A
的子类将无法重写该方法,因为它尚未标记为virtual
。另一方面,如果您的目的是在接口中将方法标记为“几乎密封”,以便它强制实现类立即密封它,这是不可能的。接口的职责不是(也不应该是)来规定此类实现细节 - 接口旨在表示规范。
I may have completely misunderstood the question, but if your intention is to seal the method in
A
, you can just do:Unlike Java, methods in C# are sealed by default. Subclasses of
A
won't be able to override the method since it hasn't been markedvirtual
.On the other hand, if your intention is to mark the method 'almost sealed' in the interface, so that it forces upon an implementing class to immediately seal it, that isn't possible. It isn't (and shouldn't be) the business of the interface to dictate such details of implementation - an interface is meant to represent a specification.
使用具有内部可见性的抽象基类。该基类在库外部不可见,但允许您密封该方法并且该类仍然实现该接口。
Use an abstract base class with internal visibility. This base class is not visible outside of the library but allows you to seal the method and the class still implements the interface.
您对
sealed
关键字的理解不正确。作为方法修饰符,sealed
用于防止虚拟
方法(在基类中定义)在下一代派生类中被重写。例如:开发人员始终可以使用 new 修饰符在派生类中定义同名的方法,该方法隐藏了基类中定义的方法。
如果“专门类”是指从A派生的类,那么答案是:他总是可以隐藏A中的方法,但他不能改变该方法的行为。
Your understanding of
sealed
keyword is incorrect. As a method modifier,sealed
is used to prevent avirtual
method(defined in the base class) to be override in the next generation of derived classes. For example:Developers can always use
new
modifier to define a method with the same name in the derived class, that hides the one defined in the base class.If "specialized class" means a class derived from A, the answer is: he can always hide the method in A, but he can't change the method's behavior.
为什么不使用如下所示的抽象类。
还没有测试过,但这应该有效吗?
Why not use an abstract class like below.
Haven't tested it but this should work?
C# 中的方法默认是密封的。这是一个示例
Methods in C# are sealed by default.. Here is a sample