类既扩展了抽象类又实现了接口

发布于 2024-07-17 07:00:09 字数 274 浏览 4 评论 0原文

如果我有一个既扩展抽象类又实现接口的类,例如:

class Example : AbstractExample, ExampleInterface
{
    // class content here
}

如何初始化此类,以便可以从接口和抽象类访问方法?

当我这样做时:

AbstractExample example = new Example();

我无法从界面访问方法。

What if I have a class that both extends an abstract class and implements an interface, for example:

class Example : AbstractExample, ExampleInterface
{
    // class content here
}

How can I initialize this class so I can access methods from both the interface and the abstract class?

When I do:

AbstractExample example = new Example();

I cannot access methods from the interface.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

心奴独伤 2024-07-24 07:00:09

您需要

  • 在 AbstractExample 中实现接口
  • 的引用

或获取对 Example
示例示例=新示例();

You need to

  • implement the interface in AbstractExample
  • or get a reference to Example


Example example = new Example();

等待圉鍢 2024-07-24 07:00:09

最后一个例子将把你与接口或抽象类的实体实例联系起来,我认为这不是你的目标。坏消息是你在这里不是使用动态类型语言,所以你只能坚持引用实体“Example”对象如先前指定的或铸造/取消铸造即:

AbstractExample example = new Example();
((IExampleInterface)example).DoSomeMethodDefinedInInterface();

您的其他选择是让 AbstractExample 和 IExampleInterface 实现一个通用接口,这样您就可以拥有 ie

abstract class AbstractExample : ICommonInterface
interface IExampleInterface : ICommonInterface
class Example : AbstractExample, IExampleInterface

现在您可以使用 ICommonInterface 并具有抽象类和您的 IExample 接口的实现。

如果这些答案都不可接受,您可能需要查看一些在 .NET 框架下运行的 DLR 语言,即 IronPython。

The last example will tie you to a solid instance of either the interface or abstract class which I presume is not your goal.The bad news is you're NOT in a dynamically typed language here, so your stuck with either having a reference to a solid "Example" objects as previously sprcified or casting/uncasting i.e:

AbstractExample example = new Example();
((IExampleInterface)example).DoSomeMethodDefinedInInterface();

Your other alternitives are to have both AbstractExample and IExampleInterface implement a common interface so you would have i.e.

abstract class AbstractExample : ICommonInterface
interface IExampleInterface : ICommonInterface
class Example : AbstractExample, IExampleInterface

Now you could work with ICommonInterface and have the functionality of both the abstract class and the implementation of your IExample interface.

If none of these answers are acceptable, you may want to look at some of the DLR languages that run under the .NET framework i.e. IronPython.

东京女 2024-07-24 07:00:09

如果您只知道抽象类,则表明您通过 Type 实例了解实际类型。 因此,您可以使用泛型:

private T SomeMethod<T>()
    where T : new(), AbstractExample, ExampleInterface
{
    T instance = new T();
    instance.SomeMethodOnAbstractClass();
    instance.SomeMethodOnInterface();
    return instance;
}

If you only know the abstract class, it suggests that you know the actual type via an instance of Type. Therefore, you could use generics:

private T SomeMethod<T>()
    where T : new(), AbstractExample, ExampleInterface
{
    T instance = new T();
    instance.SomeMethodOnAbstractClass();
    instance.SomeMethodOnInterface();
    return instance;
}
幸福丶如此 2024-07-24 07:00:09

使用:

Example example = new Example();

在更多信息后更新:

如果您确定它实现了ExampleInterface,您可以使用

AbstractClass example = new Example();
ExampleInterface exampleInterface = (ExampleInterface)example;
exampleInterface.InterfaceMethod();

您也可以通过检查接口来确保它确实实现了它

if (example is ExampleInterface) {
    // Cast to ExampleInterface like above and call its methods.
}

我不相信泛型可以帮助您,因为它们是解决了编译时间问题,如果您只有对 AbstractClass 的引用,编译器会抱怨。

编辑:或多或少是欧文所说的。 :)

Use:

Example example = new Example();

Updated after more information:

If you are sure it implements ExampleInterface, you can use

AbstractClass example = new Example();
ExampleInterface exampleInterface = (ExampleInterface)example;
exampleInterface.InterfaceMethod();

You can also make sure it really implements it by checking the interface with

if (example is ExampleInterface) {
    // Cast to ExampleInterface like above and call its methods.
}

I don't believe Generics help you as those are resolved compile time and if you only have a reference to the AbstractClass the compiler will complain.

Edit: So more or less what Owen said. :)

柏林苍穹下 2024-07-24 07:00:09

我认为这个例子会对你有所帮助:

public interface ICrud
{
    void Add();
    void Update();
    void Delete();
    void Select();
}

public abstract class CrudBase
{
    public void Add()
    {
        Console.WriteLine("Performing add operation...");
        Console.ReadLine();
    }

   public void Update()
    {
        Console.WriteLine("Performing update operation...");
        Console.ReadLine();
    }
    public void Delete()
    {
        Console.WriteLine("Performing delete operation...");
        Console.ReadLine();
    }
    public void Select()
    {
        Console.WriteLine("Performing select operation...");
        Console.ReadLine();
    }
}

public class ProcessData : CrudBase, ICrud
{

}

var process = new ProcessData();
process.Add();

I think this example will help you:

public interface ICrud
{
    void Add();
    void Update();
    void Delete();
    void Select();
}

public abstract class CrudBase
{
    public void Add()
    {
        Console.WriteLine("Performing add operation...");
        Console.ReadLine();
    }

   public void Update()
    {
        Console.WriteLine("Performing update operation...");
        Console.ReadLine();
    }
    public void Delete()
    {
        Console.WriteLine("Performing delete operation...");
        Console.ReadLine();
    }
    public void Select()
    {
        Console.WriteLine("Performing select operation...");
        Console.ReadLine();
    }
}

public class ProcessData : CrudBase, ICrud
{

}

var process = new ProcessData();
process.Add();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文