public static void Main() { try { // Declare a class instance box1: PrincipalMain box1 = new PrincipalMain(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()); } catch (Exception exc) { Console.WriteLine(exc); } finally { Console.ReadKey(true); } }
//This sample demonstrates how to explicitly implement interface members and how to access those members from interface instances. static void Main(string[] args) { // Declare a class instance "myBox": Box myBox = new Box(30.0f, 20.0f); // Declare an interface instance "myDimensions": IDimensions myDimensions = (IDimensions)myBox; // Print out the dimensions of the box: /* The following commented lines would produce compilation * errors because they try to access an explicitly implemented * interface member from a class instance: */ //System.Console.WriteLine("Length: {0}", myBox.Length()); //System.Console.WriteLine("Width: {0}", myBox.Width()); /* Print out the dimensions of the box by calling the methods * from an instance of the interface: */ System.Console.WriteLine("Length: {0}", myDimensions.Length()); System.Console.WriteLine("Width: {0}", myDimensions.Width()); // Declare an instance of the English units interface: IEnglishDimensions eDimensions = (IEnglishDimensions)myBox; // Declare an instance of the metric units interface: IMetricDimensions mDimensions = (IMetricDimensions)myBox; // 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()); Console.Read(); }
static void Main(string[] args) { Box myBox = new Box(30.0f, 20.0f); IEnglishDimensions eDimensions = (IEnglishDimensions)myBox; IMetricDimensions mDimensions = (IMetricDimensions)myBox; Console.WriteLine("Length(in):{0}:", eDimensions.Length()); Console.WriteLine("Width(in):{0}:", eDimensions.Width()); Console.WriteLine("Length(cm):{0}:", mDimensions.Length()); Console.WriteLine("Width(cm):{0}:", mDimensions.Width()); Console.Read(); }
public static void Demo() { Box myBox = new Box(30.0f, 20.0f); IEnglishDimensions eDimensions = (IEnglishDimensions)myBox; IMetricDimensions mDimensions = (IMetricDimensions)myBox; 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()); }
//主程序 static void Main(string[] args) { //定义一个实类对象"myBox": Box myBox = new Box(30.0f, 20.0f); //定义一个接口"eDimensions" IEnglishDimensions eDimensions = myBox; //定义一个接口"'mDimensions" IMetricDimensions mDimensions = (IMetricDimensions)myBox; //输出 Console.WriteLine("同时隐式、显式接口实现"); Console.WriteLine(" Length(in): {0}", eDimensions.Length()); Console.WriteLine(" Width (in): {0}", eDimensions.Width()); Console.WriteLine(" Length(cm): {0}", mDimensions.Length()); Console.WriteLine(" Width (cm): {0}", mDimensions.Width()); Console.WriteLine("同时隐式接口实现"); Console.WriteLine(" Length(in): {0}", eDimensions.Length()); Console.WriteLine(" Width (in): {0}", eDimensions.Width()); Console.WriteLine(" Length(cm): {0}", mDimensions.Length_1()); Console.WriteLine(" Width (cm): {0}", mDimensions.Width_1()); Console.Read(); }
public static void Main() { // Declare a class instance "myBox": Box myBox = new Box(30.0f, 20.0f); // Declare an instance of the English units interface: IEnglishDimensions eDimensions = (IEnglishDimensions)myBox; // Declare an instance of the metric units interface: IMetricDimensions mDimensions = (IMetricDimensions)myBox; // 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()); }
public static void Main() { // 声明类实例“myBox”: Box myBox = new Box(30.0f, 20.0f); // 声明英制单位接口的实例: IEnglishDimensions eDimensions = (IEnglishDimensions)myBox; // 声明公制单位接口的实例: IMetricDimensions mDimensions = (IMetricDimensions)myBox; // 以英制单位打印尺寸: 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()); }
public static void Main() { // Объявление экземпляра класса "myBox": Box myBox = new Box(30.0f, 20.0f); // Объявление экземпляра интерфейса английской системы мер: IEnglishDimensions eDimensions = (IEnglishDimensions)myBox; // Объявление экземпляра интерфейса метрической системы мер: IMetricDimensions mDimensions = (IMetricDimensions)myBox; // Печать размеров в единицах английской системы мер: 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()); }
public static void Show() { // 声明类实例“myBox”: MultiInterfaceDemoBox myBox = new MultiInterfaceDemoBox(30.0f, 20.0f); // 声明英制单位接口的实例: IEnglishDimensions eDimensions = (IEnglishDimensions)myBox; // 声明公制单位接口的实例: IMetricDimensions mDimensions = (IMetricDimensions)myBox; // 以英制单位打印尺寸: System.Console.WriteLine("IEnglishDimensions Length(in): {0}", eDimensions.Length()); System.Console.WriteLine("IEnglishDimensions Width (in): {0}", eDimensions.Width()); // 以公制单位打印尺寸: System.Console.WriteLine("IMetricDimensions Length(cm): {0}", mDimensions.Length()); System.Console.WriteLine("IMetricDimensions Width (cm): {0}", mDimensions.Width()); }
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 = box1; // Declare an instance of the metric units interface: IMetricDimensions mDimensions = 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()); }
static void Main(string[] args) { SampleClass sc = new SampleClass(); IControl ctrl = (IControl)sc; ISurface srfc = (ISurface)sc; // The following lines all call the same method. sc.Paint(); ctrl.Paint(); srfc.Paint(); SampleClass2 obj = new SampleClass2(); //obj.Paint(); // Compiler error. IControl c = (IControl)obj; c.Paint(); // Calls IControl.Paint on SampleClass. ISurface s = (ISurface)obj; s.Paint(); // Calls ISurface.Paint on SampleClass. //======================================================= // 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()); //======================================================= Box2 box2 = new Box2(30.0f, 20.0f); IMetricDimensions mDimensions2 = (IMetricDimensions)box2; System.Console.WriteLine("Length(in): {0}", box2.Length()); System.Console.WriteLine("Width (in): {0}", box2.Width()); System.Console.WriteLine("Length(cm): {0}", mDimensions2.Length()); System.Console.WriteLine("Width (cm): {0}", mDimensions2.Width()); //======================================================= // Declare a class instance box1: Box3 box3 = new Box3(30.0f, 20.0f); // Declare an interface instance dimensions: IDimensions dimensions = (IDimensions)box3; // The following commented lines would produce compilation // errors because they try to access an explicitly implemented // interface member from a class instance: //System.Console.WriteLine("Length: {0}", box1.getLength()); //System.Console.WriteLine("Width: {0}", box1.getWidth()); // Print out the dimensions of the box by calling the methods // from an instance of the interface: System.Console.WriteLine("Length: {0}", dimensions.getLength()); System.Console.WriteLine("Width: {0}", dimensions.getWidth()); }