Ylbtech-microsoft-csharpsamples:ylbtech-languagesamples-explicitinterface (Display interface) |
1.A, example (sample) back to top |
An explicit interface Implementation example
This example demonstrates how to explicitly implement an interface member and how to access those members from an interface instance. For background information, see Interfaces (C # Programming Guide).
Safety Instructions |
This code example is provided to illustrate a concept that does not represent the most secure coding practice, so you should not use this code example in an application or Web site. Microsoft assumes no responsibility for incidental or consequential damages arising from the use of this code sample for other purposes. |
To build and run an explicit interface implementation sample in Visual Studio
In Solution Explorer, right-click the ExplicitInterface1 project and select Set as Startup Project.
On the Debug menu, click Start Without Debugging.
Repeat the previous steps for ExplicitInterface2.
To build and run an explicit interface implementation sample from the command line
Use the "Change Directory" command to go to the "ExplicitInterface1" directory.
Type the following command:
CSC explicit1.csexplicit1 |
Use the "Change Directory" command to go to the "ExplicitInterface2" directory.
Type the following command:
CSC explicit2.csexplicit2 |
1.B, sample code 1 (sample codes) back to top |
1.b.1, Explicit1.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Explicit1.csInterfaceidimensions {floatLength (); floatWidth ();}classbox:idimensions {floatlengthinches; floatwidthinches; PublicBox (floatLengthfloatwidth) {lengthinches=length; Widthinches=width; } //an explicit interface member implementation: floatidimensions.length () {returnlengthinches; } //an explicit interface member implementation: floatidimensions.width () {returnwidthinches; } Public Static voidMain () {//declaring the class instance "Mybox":Box Mybox =NewBox (30.0f,20.0f); //declaring the interface instance "Mydimensions":Idimensions mydimensions =(idimensions) Mybox; //print out the size of the box: /*The following comment lines produce compilation errors because they attempt 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 ()); /*invoke the method from an instance of the interface to print out the size of the box:*/System.Console.WriteLine ("Length: {0}", Mydimensions.length ()); System.Console.WriteLine ("Width: {0}", Mydimensions.width ()); }}
View Code
1.b.2,
1.B, sample code 2 (sample codes) back to top |
1.b.1, Explicit2.cs
//Copyright (C) Microsoft Corporation. All rights reserved. //This code is published in compliance with//Microsoft Public License (MS-PL,http://opensource.org/licenses/ms-pl.html) of the terms. ////Copyright (C) Microsoft Corporation. All rights reserved. //Explicit2.cs//To declare an imperial unit interface:InterfaceIEnglishDimensions {floatLength (); floatWidth ();}//To declare a metric unit interface:InterfaceIMetricDimensions {floatLength (); floatWidth ();}//declares a "box" class that implements the following two interfaces://ienglishdimensions and IMetricDimensions:classbox:ienglishdimensions, imetricdimensions {floatlengthinches; floatwidthinches; PublicBox (floatLengthfloatwidth) {lengthinches=length; Widthinches=width; }//to explicitly implement IEnglishDimensions members: floatienglishdimensions.length () {returnlengthinches; } floatienglishdimensions.width () {returnwidthinches; }//to explicitly implement IMetricDimensions members: floatimetricdimensions.length () {returnLengthinches *2.54f; } floatimetricdimensions.width () {returnWidthinches *2.54f; } Public Static voidMain () {//declaring the class instance "Mybox":Box Mybox =NewBox (30.0f,20.0f); //to declare an instance of an imperial unit interface:IEnglishDimensions edimensions =(ienglishdimensions) Mybox; //declare an instance of the Metric unit interface:IMetricDimensions mdimensions =(imetricdimensions) Mybox; //print Dimensions in imperial units:System.Console.WriteLine ("Length (in): {0}", Edimensions.length ()); System.Console.WriteLine ("Width (in): {0}", Edimensions.width ()); //print size in metric units:System.Console.WriteLine ("Length (cm): {0}", Mdimensions.length ()); System.Console.WriteLine ("Width (cm): {0}", Mdimensions.width ()); }}
View Code
1.b.2,
1.C, (free Download) back to top |
|
Ylbtech Source: http://ylbtech.cnblogs.com/ This article is copyright to the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility. |
Ylbtech-languagesamples-explicitinterface (Display interface)