C# 抽象类和接口的多重继承
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11358976/
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
Multiple inheritance with Abstract class and Interface
提问by techmad
I have written the following code in C#.NET
我在 C#.NET 中编写了以下代码
public interface IWork
{
void func();
}
public abstract class WorkClass
{
public void func()
{
Console.WriteLine("Calling Abstract Class Function");
}
}
public class MyClass:WorkClass,IWork
{
}
On compiling, I didn't get any error. Compiler is not forcing me to implement the method "func();" in "MyClass", which has been derived from the interface "IWork".Latter, I can gracefully create a instance of the class "MyClass" and call the function "func()". Why the compiler is not forcing me to implement the "func()" method in the "MyClass"(which has been derived from "IWork" interface? Is it a flaw in C#?
在编译时,我没有收到任何错误。编译器并没有强迫我实现方法“func();” 在从接口“IWork”派生的“MyClass”中,我可以优雅地创建类“MyClass”的实例并调用函数“func()”。为什么编译器不强迫我在“MyClass”中实现“func()”方法(它已经从“IWork”接口派生出来?它是C#中的一个缺陷吗?
回答by Filip Ekberg
Because the abstractclass implements the interface.
因为abstract类实现了接口。
If your class MyClasswould not inherit from WorkClassyou would get an error saying 'MyClass' does not implement interface member 'IWork.func()'.
如果您的类MyClass不继承自WorkClass您,则会收到错误消息'MyClass' does not implement interface member 'IWork.func()'.
But you also inherit from WorkClass, which actually implements the methods that the interface requires.
但是您也继承自WorkClass,它实际上实现了接口所需的方法。
You can mark func()as abstract if you want to force the classes that inherits from it to implement it like this:
func()如果要强制从它继承的类像这样实现它,则可以将其标记为抽象:
public abstract class WorkClass
{
public abstract void func();
}
回答by DaveShaw
func()is not marked as abstractin the WorkClassso you don't need to implement it in any classes that derive from WorkClass.
func()未标记为abstractin,WorkClass因此您无需在任何派生自 的类中实现它WorkClass。
WorkClassimplements the IWorkinterface so you don't need to implement it in MyClassbecause it inherits func()from WorkClass.
WorkClass实现IWork接口,所以你不需要实现它,MyClass因为它继承func()自WorkClass.
回答by Blindsniper
Since the func method in the abstract class is a non-virtual method, so the compiler thinks this method is a implementation of the interface.
由于抽象类中的func方法是非虚方法,所以编译器认为这个方法是接口的实现。
回答by Vizard
When you extend MyClassto WorkClass, the method func()(which has been defined), is inherited.
当您扩展MyClass到 时WorkClass,该方法func()(已定义)将被继承。
So, when the interface IWorkis implemented, the method 'func()' has already been defined. So, there are no more undefined methods in MyClass.
因此,在实现接口时IWork,已经定义了方法“func()”。所以,在MyClass.
So, the class MyClassis a concrete class, due to which you are able to create a MyClassobject without any compilation errors.
因此,该类MyClass是一个具体类,因此您可以创建一个MyClass对象而不会出现任何编译错误。
回答by Marcus
To better understand the concept behind interfaces, I just give you the correct code of your implementation:
为了更好地理解接口背后的概念,我只给你正确的实现代码:
public interface IWork{
void func();
}
public abstract class WorkClass,IWork{
public void func(){
Console.WriteLine("Calling Abstract Class Function");
}
}
public class MyClass:WorkClass{
...
}
The basic rule: You need to include the interface always where the implementation is. So if you create a method within an abstract classes and define an interface of this method, you'll need to implement the interface into your abstract class and then all subclasses will automatically implement this interface.
基本规则:您需要始终在实现所在的位置包含接口。因此,如果您在抽象类中创建方法并定义该方法的接口,则需要将该接口实现到您的抽象类中,然后所有子类将自动实现该接口。
As a matter of fact, interfaces have 2 kind of functions you can use them for:
事实上,接口有两种功能,您可以将它们用于:
1) As a "real" interface providing a generic handling of any class implementing the interface, so you can handle several kind of classes just by one interface (without knowing their real class names). While "handling" means: Calling a method.
1) 作为一个“真实的”接口,提供对实现该接口的任何类的通用处理,因此您可以仅通过一个接口处理多种类(无需知道它们的真实类名)。而“处理”的意思是:调用一个方法。
2) As a help for other (framework) programmers not to mess up with your code. If you want to be sure that an method won't be replaced with another name, you define an interface for your class containing all "must have" method names. Even if the method is called nowhere, your programmer will get an compile error message when he changed the method name.
2)作为其他(框架)程序员的帮助,不要弄乱您的代码。如果您想确保一个方法不会被另一个名称替换,您可以为您的类定义一个接口,其中包含所有“必须具有”的方法名称。即使在任何地方都没有调用该方法,您的程序员在更改方法名称时也会收到编译错误消息。
Now you can easily handle your Myclassjust by the interface IWork.
现在您可以Myclass通过界面轻松处理您的问题IWork。
回答by Pardha Saradhi Vanjarpau
I tried with the classes above in my solution. It inherits the abstract class so the derived class have the func() method definition. This is the reason it was not able to show compiled errors.
我在我的解决方案中尝试了上面的类。它继承了抽象类,因此派生类具有 func() 方法定义。这就是它无法显示编译错误的原因。
回答by Patrick
While reading about this subject, I found I couldn't easily put it all together in my head, so I wrote the following piece of code, which acts as a cheat-sheet for how C# works. Hope it helps someone.
在阅读有关此主题的内容时,我发现我无法轻松地将所有内容放在一起,因此我编写了以下代码段,作为 C# 工作原理的备忘单。希望它可以帮助某人。
public interface IMyInterface
{
void FunctionA();
void FunctionB();
void FunctionC();
}
public abstract class MyAbstractClass : IMyInterface
{
public void FunctionA()
{
Console.WriteLine( "FunctionA() implemented in abstract class. Cannot be overridden in concrete class." );
}
public virtual void FunctionB()
{
Console.WriteLine( "FunctionB() implemented in abstract class. Can be overridden in concrete class." );
}
public abstract void FunctionC();
}
public class MyConcreteClass : MyAbstractClass, IMyInterface
{
public override void FunctionB()
{
base.FunctionB();
Console.WriteLine( "FunctionB() implemented in abstract class but optionally overridden in concrete class." );
}
public override void FunctionC()
{
Console.WriteLine( "FunctionC() must be implemented in concrete class because abstract class provides no implementation." );
}
}
class Program
{
static void Main( string[] args )
{
IMyInterface foo = new MyConcreteClass();
foo.FunctionA();
foo.FunctionB();
foo.FunctionC();
Console.ReadKey();
}
}
Gives the following output:
给出以下输出:
FunctionA() implemented in abstract class. Cannot be overridden in concrete class.
FunctionA() 在抽象类中实现。不能在具体类中被覆盖。
FunctionB() implemented in abstract class. Can be overridden in concrete class.
FunctionB() 在抽象类中实现。可以在具体类中被覆盖。
FunctionB() implemented in abstract class but optionally overridden in concrete class.
FunctionB() 在抽象类中实现,但可选地在具体类中覆盖。
FunctionC() must be implemented in concrete class because abstract class provides no implementation.
FunctionC() 必须在具体类中实现,因为抽象类不提供实现。

