C # Programming Weapon III: Interface (INTERFACE)

Source: Internet
Author: User
Tags definition

The C # interface is something that allows many beginners to get confused, to use it as if it's simple, to define an interface, and then to define a method inside it, and then implement it by inheriting the subclass. But when you do not really understand the role of the interface is to use the interface is superfluous, of course, you think this is absolutely wrong. In software design, there is a very important principle is: interface programming, dependency and interface or abstraction layer. The visible interface is so important in real development.

In the previous C # programming weapon One: Class (classes) in the article introduced the relevant knowledge of the class, this article mainly introduces OO programming in another important point of knowledge-interface. In a way, an interface is also a class, a special class, or an abstract class. More accurately, the interface contains only the signature of a method, delegate, or event. The implementation of the method is done in the class that implements the interface [MSDN].

I. Definition of interface

As defined on the interface on MSDN, the interface contains only the signature of the method, delegate, or event. The more popular explanation for this sentence is that the interface is only responsible for completing the defined operation without implementing specific details. As the following iplayer interface, it is a game interface, which only defines the corresponding method, without the specific implementation of the method, the code is as follows:

1/**//// <summary>
2/// 玩游戏接口
3/// </summary>
4public interface IPlayer
5{
6  /**//// <summary>
7  /// 获取玩家的名字
8  /// </summary>
9  /// <returns>玩家的名字</returns>
10  string GetName();
11
12  /**//// <summary>
13  /// 由Player决定出什么手势
14  /// </summary>
15  /// <returns>本接口定义的三个常量之一</returns>
16  string Show();
17}

The above is the definition of a typical interface. Defines an interface named iplayer, which internally defines two methods GetName and show. In addition to defining methods in the interface, we can also define attributes, indexes, events, etc., please look at the definition on MSDN or related books, here take the attribute as an example of a simple introduction, in the interface can only be defined not implemented, the specific implementation is given to its subclasses to complete, then the property should be how to define it?

Usually we define the attributes as follows:

1/**//// <summary>
2/// 定义_Name属性,并提供get;set属性访问器
3/// </summary>
4private string _Name;
5public string Name
6{
7  get { return _Name; }
8  set { _Name = value; }
9}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.