首页 > 开发 > 综合 > 正文

Multiple Implementation in C#

2024-07-21 02:22:21
字体:
来源:转载
供稿:网友
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.
中国最大的web开发资源网站及技术社区,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表