程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> Multiple Implementation in C#

Multiple Implementation in C#

編輯:關於C語言
Multiple Implementation in C#
By Craig Breakspear
Can you inherit from multiple classes in C#? Simply put, this cannot be done. However there are ways around it. From a design perspective you must ask yourself, Will a Class fully represent an object? Meaning that, if we have a base class with abstract methods designed for a particular application and we know that the inheriting object will only need the methods defined in that class. We now have a valid design pattern here.

The Vehicle Car Object

Lets say we have an abstract class called "Vehicle" as well as another class called "ConstructionVehicle". The vehicle class has methods such as Accelerate() , Stop(), and the "ConstructionVehicle" class has methods such as ExecuteDump() and TurnOnBackUpSound(). If we were only going to build a Car object and know we would only use those methods from the "Automobile" class this would be fine.

The DumpTruck Object

Now we want to create another object called "DumpTruck". We could inherit from the Automobile class but that class does not have the methods that we need called ExecuteDump() and TurnOnBackUpSound(). If we were using a language such as C++ we could easily inherit from both classes using multiple inheritance. However, seeing C# is our language of choice, multiple inheritance is not an option, you may only inherit from one Base Class.

From Abstract Classes to Interfaces


From a design perspective we must choose a different design. C# supports what is called "Multiple Implementation", which is to says a class can implement more than one interface. Our design now changes the "Vehicle" class and the "ConstructionVehicle" class into interfaces. Below we have defined the two interfaces with their very simplistic methods:

IE:

interface IConstructionVehicle
{
void ExecuteDump();
void TurnOnBackUpSound();
}
interface IVehicle
{
void Accelerate();
void Stop();
void TurnOnBackUpSound();
}

If we built a class that inherited from these two interfaces we would be able to do so spanning multiple inherited interfaces. Design problem solved! Or is it?
Explicit Interface Implementation

If you look at both interfaces defined above you'll notice that they share in common a method of the same name "TurnOnBackUpSound()". Problem? No, in fact C# supports what is known as "Explicit Interface Implementation", which allows the programmer to specify which member of which interface they want to use. Putting the Interface name in front of the member name allows this to happen as shown below.

public class DumpTruck: IEngine, IBody
{

void IEngine.Test()
{
  Console.WriteLine("This is the Engine TEst");
}
void IBody.Test()
{
  Console.WriteLine("This is the Body TEst");
}
}

Implementation Hiding
Another benefit to this technique is something called "Implementation Hiding". Implementation Hiding allows the methods from the implemented interface to be hidden from the derived class unless the developer explicitly calls the interface. This technique obviously reduces the clutter for a developer.
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved