In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to use interface in C#. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.
C # interface is a collection that combines the required members to encapsulate certain functions. It is like a template that defines the members that an object must implement and implements it through a class or structure. The interface cannot be instantiated directly, that is, ICount ic=new iCount () is wrong. The interface cannot contain any code for the member, only the member itself is defined. The specific code of the interface member is provided by the class that implements the interface. The interface is declared using the interface keyword. The format of the declaration is as follows:
[attributes] [modifiers] interface identifier [: base-list] {interface-body} {;}
The default access method for C # interface members is public, and keywords such as abstract, public, protected, internal, private, virtual, override or static cannot appear when declaring interface members. Interface members can be methods, properties, index indicators, or events, not fields, and the interface member names cannot be the same.
The default access method for using System; using System.Collections.Generic; using System.Text; namespace Interface {interface ICount {void Count (); / / interface members is that the field member int para {get;set;}} class Double cannot be defined in the public / / int number;// interface: ICount {public void Count () {/ / implement the Count () method Console.WriteLine ("The double para is {0}", 2*para) of ICount;} int p Public int para {get {return p;} set {p = value;} class Program {static void Main (string [] args) {Double d = new Double (); d.para = 10 args / assign d.Count () to attribute; ICount ic = (ICount) dbank / convert to interface ic.para = 5; ic.Count (); Console.ReadLine ();}
Summary of the use of C # interface
1 A class can implement more than one interface
Class 2 must implement "all" properties and methods in the interface
3 the format of the property and method definition must be exactly the same as that of the interface definition. The number and type of parameters taken by the method must be exactly the same as those in the interface. The name of the method must be the same.
Inheritance between interfaces: the inheritance of interfaces only illustrates the inheritance relationship between interfaces, and the derived interface inherits the member description of the parent interface, not the implementation of the parent interface. Interfaces of private and internal types do not allow inheritance. If the method of the parent interface is to be overridden in the derived interface, the override of inherited members in the same way is implemented.
If a class implements an interface, the class implicitly inherits all the parent interfaces of the interface, even if the parent interface is not listed in the class's base class table.
If two interfaces An and B contain a member Method of the same name, and both are implemented by the same class C, class C must provide a separate implementation for the Method members of An and B, that is, explicitly implement the interface member. Feasible options:
(1) directly implement the members of one interface and explicitly implement the members of another interface
(2) explicitly implement the members of the two interfaces
Using System; using System.Collections.Generic; using System.Text; namespace Interface {interface IEnglish {float Length (); float Width ();} interface IMetric {float Length (); float Width ();} class Class1: IEnglish, IMetric {float lengthInches; float widthInches; public Class1 (float length, float width) {lengthInches = length; widthInches = width;} / / explicit implementation of IEnglish member float IEnglish.Length () {return lengthInches } float IEnglish.Width () {return widthInches;} / / explicitly implement members of IMetric float IMetric.Length () {return lengthInches * 2.54f;} float IMetric.Width () {return widthInches * 2.54f;} static void Main (string [] args) {Class1 C1 = new Class1 (30.0fZOOF 20.0f); IEnglish e = (IEnglish) C1; IMetric m = (IMetric) C1 Console.WriteLine ("Length (in): {0}", e.Length ()); Console.WriteLine ("Width (in): {0}", e.Width ()); Console.WriteLine ("Length (cm): {0}", m.Length ()); Console.WriteLine ("Width (cm): {0}", m.Width ()); Console.ReadLine () } this is the end of this article on "how to use interface in C#". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.