Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 类既扩展了抽象类,又实现了接口_C#_.net_Oop_Interface_Abstract Class - Fatal编程技术网

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

C# 类既扩展了抽象类,又实现了接口,c#,.net,oop,interface,abstract-class,C#,.net,Oop,Interface,Abstract Class,如果我有一个既扩展抽象类又实现接口的类,例如: class Example : AbstractExample, ExampleInterface { // class content here } 如何初始化该类,以便从接口和抽象类访问方法 当我这样做时: AbstractExample example = new Example(); 我无法从界面访问方法。使用: Example example = new Example(); 更多信息后更新: 如果您确定它实现了Exampl

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

class Example : AbstractExample, ExampleInterface
{
    // class content here
}
如何初始化该类,以便从接口和抽象类访问方法

当我这样做时:

AbstractExample example = new Example();
我无法从界面访问方法。

使用:

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.
}
我不相信泛型对您有帮助,因为它们是在编译时解析的,如果您只有对抽象类的引用,编译器会抱怨

编辑:欧文说的差不多吧。:)

你需要

  • 在AbstractExample中实现接口
  • 或者参考一个例子

示例=新示例();

如果您只知道抽象类,它建议您通过
类型的实例知道实际类型。因此,您可以使用泛型:

private T SomeMethod<T>()
    where T : new(), AbstractExample, ExampleInterface
{
    T instance = new T();
    instance.SomeMethodOnAbstractClass();
    instance.SomeMethodOnInterface();
    return instance;
}
private T SomeMethod()
其中T:new(),AbstractExample,ExampleInterface
{
T实例=新的T();
实例。SomeMethodOnAbstractClass();
instance.SomeMethodOnInterface();
返回实例;
}

最后一个示例将您绑定到接口或抽象类的实体实例,我认为这不是您的目标。坏消息是您在这里不是使用动态类型化语言,因此您必须像以前指定的那样引用实体“示例”对象,或者强制/取消强制播放,即:

AbstractExample example = new Example();
((IExampleInterface)example).DoSomeMethodDefinedInInterface();
您的其他选择是让AbstractExample和IExampleInterface实现一个公共接口,这样您就可以

abstract class AbstractExample : ICommonInterface
interface IExampleInterface : ICommonInterface
class Example : AbstractExample, IExampleInterface
现在,您可以使用ICommonInterface并拥有抽象类和IExample接口实现的功能


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

我认为这个例子将帮助您:

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();

非常感谢。但是如果我不知道类的引用,我只知道它扩展了哪个抽象类和实现了哪个接口,那该怎么办呢?理查兹:谢谢你,你的两个答案都很有用;)我对C有点困惑,因为我来自PHP/Zend,它是一种非常不同的语言。是的,我想很多人也有这种背景(包括我)。您可以使用静态语言完成动态语言无法完成的任务,反之亦然。最好的办法是理解差异,看看哪条线会稍微模糊。NET 3.5引入了一些使语言“dynamicish”的强大功能,Kent Boogaart的解决方案是一个很好的开端,也许可以在不需要额外接口的情况下获得您想要的功能。C#不是一种动态类型语言。此外,您没有提供最简单的答案,即Example Example=new Example()@保罗·巴图姆和诺尔多林:纯粹的打字错误,不值得一票否决,只是错过了“不”@Paul Batum-你没有抓住要点,帖子显然不希望与示例类有很强的依赖性,因此他对示例类进行了接口和抽象。@Owen:Up投票。这非常有帮助。而且,有时候,人们在否决票上可能太快了。这句话的上下文应该给人一个线索,说明有打字错误。:)我非常喜欢这个答案,现在我的脑子里在想如何进一步解决这个问题。
ProcessData()
是从哪里来的?除了实例化之外,没有提到它。@McArthey,我只是为你编辑了我的答案。首先创建实现类并继承基类,然后继承ICrud接口。感谢您花时间澄清。很不错的。