Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Talking about Interface

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

API: take a look at the following paragraph:

If you're about to start programming, it feels like you need a lot of classes, and some of them, you already know in advance what common features you need. What shall I do? Of course, the first thought is to give them a model in advance, stipulating what kind of method they are, whether there is a return value, or no return value, what type to return, and so on.

The interface can help you. Interface, just a kind of specification, a kind of constraint.

Concept: an interface is a collection of methods that combine to encapsulate specific functions. Note:

1. Once the class implements the interface, the class can support all the properties and members specified by the interface.

two。 Declaring an interface is syntactically identical to declaring an abstract class, but concrete implementations of any members are not allowed in the interface. Therefore, interfaces cannot be instantiated. There can be no constructors and fields.

3. There can be no modifiers, such as public,private, and cannot be declared virtual or static.

4. The class that implements the interface must implement all the methods in the interface. (similar to abstract methods in abstract classes that must be fully implemented.)

5. A class can support the implementation of multiple interfaces, and multiple classes can support the same interface.

6. The naming of the interface should be preceded by an uppercase "I".

Usage:

The interface is declared with interface, but not class. The interface name should be preceded by "I". Properties or methods in the interface cannot have modifiers, methods, or method bodies.

Code example: the situation of 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849interface IPlay {/ / int _ id; is wrong, because it is true that there can be no field int Num / / in the interface, because it is a property, and the essence of the property is a get,set of two methods. {get; set;} void PlayGame (); / / as long as it is defined, the class that implements the interface must implement the method void PlayBasketball ();} interface IEat {void EatFruit ();} class Man:IPlay,IEat / / A class can implement multiple interfaces, separated by commas, and inherit classes, but only one. You can try it yourself. {the public int Num / / attribute is also to be implemented, because it is the method, and fields are not allowed. You can try. {get; set;// is abbreviated here. If you don't know what the attribute is, you can check what the attribute is. Again, these are two methods} public void PlayGame () {Console.WriteLine ("I can PlayGame");} public void PlayBasketball () {Console.WriteLine ("I can Play BasketBall");} public void EatFruit () {Console.WriteLine ("I Can EatFruit"). }-static void Main (string [] args) {Man m = new Man (); m.EatFruit (); m.Num = 2; m.PlayBasketball (); m.PlayGame (); Console.Read ();}

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.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report