C# 类既扩展了抽象类又实现了接口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/789499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class both extends an abstract class and implements an interface
提问by Richard Knop
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.
我无从接口访问方。
采纳答案by Owen
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:
最后一个例子将把你绑定到接口或抽象类的一个实体实例,我认为这不是你的目标。坏消息是你在这里不是使用动态类型语言,所以你坚持要么引用一个固体“示例”对象,如先前 sprcified 或铸造/取消铸造,即:
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.
您的其他替代方案是让 AbstractExample 和 IExampleInterface 都实现一个通用接口,因此您将拥有即
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.
现在您可以使用 ICommonInterface 并拥有抽象类和 IExample 接口的实现的功能。
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.
如果这些答案都不可接受,您可能需要查看一些在 .NET 框架下运行的 DLR 语言,即 IronPython。
回答by Mikko Rantanen
Use:
用:
Example example = new Example();
Updated after more information:
在更多信息后更新:
If you are sureit implements ExampleInterface, you can use
如果您确定它实现了 ExampleInterface,则可以使用
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.
我不相信泛型对你有帮助,因为那些是在编译时解决的,如果你只有对 AbstractClass 的引用,编译器会抱怨。
Edit: So more or less what Owen said. :)
编辑:或多或少是欧文所说的。:)
回答by Stefan Steinegger
You need to
你需要
- implement the interface in AbstractExample
- or get a reference to Example
- 在 AbstractExample 中实现接口
- 或获取对示例的引用
Example example = new Example();
Example example = new Example();
回答by Kent Boogaart
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:
如果您只知道抽象类,则表明您通过Type
. 因此,您可以使用泛型:
private T SomeMethod<T>()
where T : new(), AbstractExample, ExampleInterface
{
T instance = new T();
instance.SomeMethodOnAbstractClass();
instance.SomeMethodOnInterface();
return instance;
}
回答by Toffee
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();