C#中接口不可以实例化的问题
发表在C#图书答疑
2009-02-05
是否精华
是
否
版块置顶:
是
否
interface IEnglishDimensions {
float Length ( ) ;
float Width ( ) ;
}
interface IMetricDimensions {
float Length ( ) ;
float Width ( ) ;
}
class Box : IEnglishDimensions, IMetricDimensions {
float lengthInches ;
float widthInches ;
public Box(float length, float width) {
lengthInches = length ;
widthInches = width ;
}
float IEnglishDimensions.Length( ) {
return lengthInches ;
}
float IEnglishDimensions.Width( ) {
return widthInches ;
}
float IMetricDimensions.Length( ) {
return lengthInches * 2.54f ;
}
float IMetricDimensions.Width( ) {
return widthInches * 2.54f ;
}
public static void Main( ) {
Box myBox = new Box(30.0f, 20.0f);
// 定义一个接口” eDimensions”::
//IEnglishDimensions eDimensions = (IEnglishDimensions) myBox;
IEnglishDimensions eDimensions = myBox;
//IEnglishDimensions eDimensions;
// eDimensions = myBox;
IMetricDimensions mDimensions = (IMetricDimensions) myBox;
// 输出:
System.Console.WriteLine(" Length(in): {0}", eDimensions.Length( ));
System.Console.WriteLine(" Length(in): {0}", eDimensions.Length());
System.Console.WriteLine(" Width (in): {0}", eDimensions.Width( ));
System.Console.WriteLine(" Length(cm): {0}", mDimensions.Length( ));
System.Console.WriteLine(" Width (cm): {0}", mDimensions.Width( ));
System.Console.ReadLine();
}
}
C#中的接口不能被实例化,然而这个程序中 IEnglishDimensions eDimensions 怎么被实例化了? 我不理解,请高手解释一下。