国产探花免费观看_亚洲丰满少妇自慰呻吟_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開發資源網站及技術社區,
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宝坻区| 沁阳市| 富川| 竹山县| 宁乡县| 鸡西市| 滁州市| 祁连县| 富顺县| 聂拉木县| 综艺| 斗六市| 武强县| 布尔津县| 台东市| 鄢陵县| 武邑县| 富宁县| 漳州市| 九寨沟县| 鄂托克前旗| 江西省| 西充县| 永登县| 肃北| 鹿泉市| 唐山市| 邯郸市| 安多县| 盐源县| 县级市| 增城市| 辉县市| 长子县| 秦安县| 庆云县| 大邑县| 庆云县| 江永县| 商丘市| 白山市|