实现后密封接口

发布于 2024-10-24 13:47:07 字数 559 浏览 6 评论 0原文

我正在做一个小项目,我遇到了这个问题。

项目输出是一个包含接口的库。如果可能的话,我想实现该接口并密封其中的函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

才能让你更想念 2024-10-31 13:47:07

我可能完全误解了这个问题,但如果您的意图是将方法密封在 A 中,您可以这样做:

class A : ITest
{
    public void SomeMethod()  { ... }
}

与 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:

class A : ITest
{
    public void SomeMethod()  { ... }
}

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 marked virtual.

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.

听,心雨的声音 2024-10-31 13:47:07

使用具有内部可见性的抽象基类。该基类在库外部不可见,但允许您密封该方法并且该类仍然实现该接口。

public interface ITest
{
    void SomeMethod();
}

internal abstract class SuperA : ITest
{

    public abstract void SomeMethod();

}

class A : SuperA
{
    public sealed override void SomeMethod()
    {

    }
}

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.

public interface ITest
{
    void SomeMethod();
}

internal abstract class SuperA : ITest
{

    public abstract void SomeMethod();

}

class A : SuperA
{
    public sealed override void SomeMethod()
    {

    }
}
亽野灬性zι浪 2024-10-31 13:47:07

您对 sealed 关键字的理解不正确。作为方法修饰符,sealed 用于防止虚拟 方法(在基类中定义)在下一代派生类中被重写。例如:

class Base
{
   public virtual void M() { }
}

class Derived : Base
{
   public sealed override void M() { }
}

class A : Derived
{
   public override void M() { } //compile error, M is sealed in Derived
}

开发人员始终可以使用 new 修饰符在派生类中定义同名的方法,该方法隐藏了基类中定义的方法。

如果有人创建一个专门的类
A 型,他/她将无法
更改方法的行为。

如果“专门类”是指从A派生的类,那么答案是:他总是可以隐藏A中的方法,但他不能改变该方法的行为。

Your understanding of sealed keyword is incorrect. As a method modifier, sealed is used to prevent a virtual method(defined in the base class) to be override in the next generation of derived classes. For example:

class Base
{
   public virtual void M() { }
}

class Derived : Base
{
   public sealed override void M() { }
}

class A : Derived
{
   public override void M() { } //compile error, M is sealed in Derived
}

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 someone create a specialized class
of type A, he/she won't be able to
change the method's behavior.

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.

吻风 2024-10-31 13:47:07

为什么不使用如下所示的抽象类。

还没有测试过,但这应该有效吗?

public abstract class Test
{
    public virtual void SomeMethod() {}
    //OR
    public abstract void SomeMethod();//MSDN says:
    //an abstract method is implicitly virtual
}

class A : Test
{
    public sealed override SomeMethod()
    {

    }
}

Why not use an abstract class like below.

Haven't tested it but this should work?

public abstract class Test
{
    public virtual void SomeMethod() {}
    //OR
    public abstract void SomeMethod();//MSDN says:
    //an abstract method is implicitly virtual
}

class A : Test
{
    public sealed override SomeMethod()
    {

    }
}
通知家属抬走 2024-10-31 13:47:07

C# 中的方法默认是密封的。这是一个示例

class Program
{
    static void Main(string[] args)
    {

        A obj = new A();
        obj.SomeMethod();
        b ss = new b();
        ss.SomeMethod();
        Console.ReadLine();
    }
}

public interface ITest { void SomeMethod(); }

class A : ITest { public void SomeMethod() {

    Console.WriteLine("SomeMethod Called from Class A object");
} }

class b : A
{
    //public override void SomeMethod()
    //{
    //    Console.WriteLine("Called from Class B Object");
    //}
}

Methods in C# are sealed by default.. Here is a sample

class Program
{
    static void Main(string[] args)
    {

        A obj = new A();
        obj.SomeMethod();
        b ss = new b();
        ss.SomeMethod();
        Console.ReadLine();
    }
}

public interface ITest { void SomeMethod(); }

class A : ITest { public void SomeMethod() {

    Console.WriteLine("SomeMethod Called from Class A object");
} }

class b : A
{
    //public override void SomeMethod()
    //{
    //    Console.WriteLine("Called from Class B Object");
    //}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文