国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

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開發資源網站及技術社區,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巨鹿县| 竹北市| 宜阳县| 普安县| 泰来县| 宁陕县| 师宗县| 黄大仙区| 松江区| 铜川市| 永昌县| 汤原县| 大悟县| 东台市| 广灵县| 满洲里市| 晋州市| 花莲县| 六枝特区| 阳江市| 宁夏| 容城县| 石泉县| 张家港市| 天等县| 无极县| 中超| 宝坻区| 麦盖提县| 图片| 大港区| 江口县| 鄂州市| 辉县市| 邳州市| 永寿县| 阜康市| 大丰市| 唐山市| 襄城县| 汾西县|