繁体   English   中英

我怎样才能拥有一个实现接口并继承自抽象 class 的 class? (C# .NET 3.5)

[英]How can I have a class that implements an interface and inherits from an abstract class? (C# .NET 3.5)

我有一种情况,我认为我需要接口和抽象 class 提供的功能。

我正在创建要在模拟中使用的模型。 每个 model 都包含在 C# class 中,它实现了 IModel 接口(我的设计,因此可以修改)。 我还创建了一个 c++ 非托管 Win32 DLL 作为我的 C# Z20F35E630DAF494DBFACZ3F6 类和模拟环境之间的桥梁。 model 类通过 COM 调用,因此需要接口。

我刚刚收到一个新要求,即这些 model 类应该可以通过 web 服务独立于模拟环境进行调用。 现在这些模型相当敏感,客户端要求它们由客户端配置并在服务器上执行*。 我认为如果我让 model 类继承自抽象 class,然后在服务器上创建 web 服务方法,这将是可能的:

public AbstractModel executeModel(AbstractModel modelForExecution)

从客户端接收 model object,执行它并用结果填充它。

但是 - 我不能使用抽象的 class 因为模型需要 COM 实现的接口。

我怎样才能调和这两个要求? 有人告诉我,可以让 class 实现一个接口,并让 class 从抽象 class 派生,但我找不到任何例子。 如果可能的话,语法是什么? 如果不可能,我还能做什么?

*the model classes actually call an ancient fortran executable, so the model configuration of the classes does not give away too much information about how the model works.

public abstract class Base{}
public interface IBase{}
public Something: Base, IBase{} 
// the interface needs to go after the base class

我会尝试让您的抽象 class 实现接口(并将方法作为抽象)。 然后每个实现都只是抽象 class 的子类。 我认为这应该有效。

public interface IModel {
  void doSomething();
}

public abstract class AbstractModel : IModel {
  public abstract void doSomething();
}

public class MyModel : AbstractModel {
  public void doSomething() {
    // code goes here
  }
}

您可能必须在实现方法上使用“覆盖”关键字(编译器会告诉您)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM