public override void Show() { // Declare a class instance "myBox": Box myBox = new Box(30.0f, 20.0f); // Declare an interface instance "myDimensions": IDimensions myDimensions = (IDimensions)myBox; IMetricDimensions mIMetricDimensions = (IMetricDimensions)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()); System.Console.WriteLine("Length: {0}", mIMetricDimensions.Length()); System.Console.WriteLine("Width: {0}", mIMetricDimensions.Width()); }
//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(); }
public static int Main() { Box myBox = new Box(30.0f, 20.0f); IDimensions myDimensions = (IDimensions)myBox; float a = myBox.Length(); a += myBox.Width(); a += myDimensions.Length(); a += myDimensions.Width(); return((int)a); }
public static void Demo() { Box myBox = new Box(30.0f, 20.0f); IDimensions myDimensions = (IDimensions)myBox; // <- обр. внимание /* Так компилятор выдаст ошибку: */ //System.Console.WriteLine("Length: {0}", myBox.Length()); //System.Console.WriteLine("Width: {0}", myBox.Width()); System.Console.WriteLine("Length: {0}", myDimensions.Length()); System.Console.WriteLine("Width: {0}", myDimensions.Width()); }
public static void Main() { // Объявление экземпляра класса "myBox": Box myBox = new Box(30.0f, 20.0f); // Объявление экземпляра интерфейса "myDimensions": IDimensions myDimensions = (IDimensions)myBox; // Печать размеров окна: /* Следующие строки комментариев вызовут ошибки компиляции, поскольку они пытаются получить доступ к реализованному явным образом члену интерфейса из экземпляра класса: */ //System.Console.WriteLine("Length: {0}", myBox.Length()); //System.Console.WriteLine("Width: {0}", myBox.Width()); /* Печать размеров при помощи вызова методов из экземпляра интерфейса: */ System.Console.WriteLine("Length: {0}", myDimensions.Length()); System.Console.WriteLine("Width: {0}", myDimensions.Width()); }
public static int Main() { // Declare a class instance "myBox": Box myBox = new Box(30.0f, 20.0f); // Declare an interface instance "myDimensions": IDimensions myDimensions = (IDimensions)myBox; // Interface call through class instance float a = myBox.Length(); a += myBox.Width(); // Explicit interface method call a += myDimensions.Length(); a += myDimensions.Width(); return((int)a); }
public static void Main() { // 声明类实例“myBox”: Box myBox = new Box(30.0f, 20.0f); // 声明接口实例“myDimensions”: IDimensions myDimensions = (IDimensions)myBox; // 打印出盒子的尺寸: /* 下列注释行将产生编译 * 错误,因为这些行尝试从类实例访问显式实现的 * 接口成员: */ //System.Console.WriteLine("Length: {0}", myBox.Length()); //System.Console.WriteLine("Width: {0}", myBox.Width()); /* 从接口的实例调用方法, * 以打印出盒子的尺寸: */ System.Console.WriteLine("Length: {0}", myDimensions.Length()); System.Console.WriteLine("Width: {0}", myDimensions.Width()); }