简体   繁体   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)

I have a situation which I think I need the functionality provided by an interface and an abstract class.我有一种情况,我认为我需要接口和抽象 class 提供的功能。

I am creating models to be used in a simulation.我正在创建要在模拟中使用的模型。 Each model is contained in a C# class that implements an IModel interface (of my design so it can be modified).每个 model 都包含在 C# class 中,它实现了 IModel 接口(我的设计,因此可以修改)。 I also created a c++ unmanaged Win32 DLL that acts as a bridge between my C# model classes and the simulation environment.我还创建了一个 c++ 非托管 Win32 DLL 作为我的 C# Z20F35E630DAF494DBFACZ3F6 类和模拟环境之间的桥梁。 The model classes are called via COM, hence the requirement for an interface. model 类通过 COM 调用,因此需要接口。

I have just received a new requirement that these model classes should be callable via a web service independently of the simulation environment.我刚刚收到一个新要求,即这些 model 类应该可以通过 web 服务独立于模拟环境进行调用。 Now these models are rather sensitive, and the client has requested that they be configured by the client and executed on the server*.现在这些模型相当敏感,客户端要求它们由客户端配置并在服务器上执行*。 I figured this would be possible if I made the model classes inherit from an abstract class, then created a web service method on the server:我认为如果我让 model 类继承自抽象 class,然后在服务器上创建 web 服务方法,这将是可能的:

public AbstractModel executeModel(AbstractModel modelForExecution)

that receives a model object from the client, executes it and populates it with results.从客户端接收 model object,执行它并用结果填充它。

BUT - I cant use an abstract class because the models require an interface for the COM implementation.但是 - 我不能使用抽象的 class 因为模型需要 COM 实现的接口。

How can I reconcile these two requirements?我怎样才能调和这两个要求? I've been told that it is possible to have a class implement an interface AND have that class derive from an abstract class, but I cant find any examples of that.有人告诉我,可以让 class 实现一个接口,并让 class 从抽象 class 派生,但我找不到任何例子。 If its possible, what is the syntax?如果可能的话,语法是什么? If its not possible, what else should I do?如果不可能,我还能做什么?

*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. *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

I would try having your abstract class implement the Interface (and have the method as abstract).我会尝试让您的抽象 class 实现接口(并将方法作为抽象)。 Then each implementation just subclasses the abstract class.然后每个实现都只是抽象 class 的子类。 I think that should work.我认为这应该有效。

public interface IModel {
  void doSomething();
}

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

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

You might have to use the 'overrides' keyword on the implementation method (the compiler will tell you).您可能必须在实现方法上使用“覆盖”关键字(编译器会告诉您)。

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

相关问题 VB.NET 类继承一个基类并实现一个接口问题(在 C# 中工作) - VB.NET class inherits a base class and implements an interface issue (works in C#) 如何列出实现抽象类和接口(C#)的东西? - How to make a list of something that implements abstract class and interface (C#)? 从实现接口的抽象类继承的类,并进行反射性加载 - Class which inherits from abstract class which implements interface, and loading this reflectively 在运行时创建类型,继承抽象类并实现接口 - Create type at runtime that inherits an abstract class and implements an interface 获取一个类继承自并在 C# 中实现的所有类型和接口 - Get all types and interfaces a class inherits from and implements in C# c#类同时实现和继承 - c# class implements and inherits at the same time 继承自抽象 class 的泛型 class 类型的 C# - C# type of generic class that inherits from abstract class C#表继承Abstract类并实现接口。 - C# Form that inherit Abstract class AND implements interface. C#:我可以扩展外部类,使其实现本地接口 - C#: Can I extend a foreign class such that it implements a local interface 如何声明从另一个抽象类继承的抽象泛型类? - How can I declare an abstract generic class that inherits from another abstract class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM