C# 的接口如何实现c++ 的多继承?

hetengfei_ 2012-03-05 09:25:56
//\用C# 的接口如何实现c++ 的多继承?\//

比如://直接看代码吧!

public class MP3
{
public string value = "MP3";
public string Play()
{
return "I can play music!";
}
}
public class Mobile
{
public string value = "mobile";
public string Call()
{
return "I use to call someone!";
}
}
public class Mp3Phole
{
//如何继承 MP3 与 Mobile啊
}
...全文
257 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
lookmai 2013-07-29
  • 打赏
  • 举报
回复
多继承绝对有用,.net为何不实现?
欢乐的小猪 2012-03-07
  • 打赏
  • 举报
回复
当初看head first design patterns 第一章的时候
给我的第一感觉strategy实现了多重继承
种草德鲁伊 2012-03-07
  • 打赏
  • 举报
回复
楼主是想实现多继承还是重用啊,重用的方式很多,不一定要靠继承。
cdglynn 2012-03-07
  • 打赏
  • 举报
回复
接口多重继承不能实现具体方法的继承,这个只能用聚合,类似设计模式中的门面
http://hi.baidu.com/wujiedongsoso/blog/item/954e101da6fd1c1240341760.html
hetengfei_ 2012-03-07
  • 打赏
  • 举报
回复
不过,还是想听听高手们是如何 解决C# 的多继承的,

就是同时把两个基类的[方法] 属性copy 过来!
lihanbing 2012-03-05
  • 打赏
  • 举报
回复
多重继承是为了实现多态,不要为了多重继承而多重继承
接口同样可以实现多态
hetengfei_ 2012-03-05
  • 打赏
  • 举报
回复
现在我所理解的接口是一个 框框,好比“合同”;
这个接口定义的[方法][属性] ,就定死了 接用 他的类,必须去实现。
如比一个协议,一个要求,
要求别人去做到 自已定义的[方法]和[属性]。

根本就不是一个类, 甚至一个结构体也说不上, 倒是有点象 抽象类。

怎么可以用来搞多继承呢? 楼上的回答,听不懂啊。
种草德鲁伊 2012-03-05
  • 打赏
  • 举报
回复
除了继承,还可以聚合
threenewbee 2012-03-05
  • 打赏
  • 举报
回复
如果你希望利用接口共享方法实现的话,你可以使用扩展方法。
hetengfei_ 2012-03-05
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 zzzzv0 的回复:]
给其中一个弄个接口,然后继承一个关联一个,再实现下接口,还能咋办
[/Quote]

这都不象是个 继承 啊,难道父类中的[方法][属性] 要在子类来实现啊? 可是,父类已现实了。

下面这个是我大msdn 找到的例子:
	public class Program
{
static void Main(string[] args)
{
// Declare a class instance box1:
Box box1 = new Box(30.0f, 20.0f);

// Declare an instance of the English units interface:
IEnglishDimensions eDimensions = (IEnglishDimensions)box1;

// Declare an instance of the metric units interface:
IMetricDimensions mDimensions = (IMetricDimensions)box1;

// Print dimensions in English units:
System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
System.Console.WriteLine("Width (in): {0}", eDimensions.Width());

// Print dimensions in metric units:
System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
}

}

// Declare the English units interface:
interface IEnglishDimensions //尺寸,
{
float Length();
float Width();
}

// Declare the metric units interface:
interface IMetricDimensions //尺寸,
{
float Length();
float Width();
}

// Declare the Box class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions//这里就是多继承.
{
float lengthInches;
float widthInches;

public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}

// Explicitly implement the members of IEnglishDimensions:
float IEnglishDimensions.Length()
{
return lengthInches;
}

float IEnglishDimensions.Width()
{
return widthInches;
}

// Explicitly implement the members of IMetricDimensions:
float IMetricDimensions.Length()
{
return lengthInches * 2.54f;
}

float IMetricDimensions.Width()
{
return widthInches * 2.54f;
}
}


估计,大家不是这样写的吧, 这样来写,还不如分两次来。(建一个中间的class)
zzzzv0 2012-03-05
  • 打赏
  • 举报
回复
给其中一个弄个接口,然后继承一个关联一个,再实现下接口,还能咋办
hetengfei_ 2012-03-05
  • 打赏
  • 举报
回复
不知这个,如何用接口来模拟啊。

接口,只是一个空壳子,里面什么都没有,真的想不到,如何来实现。
threenewbee 2012-03-05
  • 打赏
  • 举报
回复
C#不支持基于类的多重继承。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧