C # Get IP Address interface programming

Source: Internet
Author: User
Tags class definition get ip modifier modifiers

Through the understanding of IP address acquisition, due to the business is not very understanding, I use interface programming to achieve GetIP ():

An interface is like a template that defines the methods that an object must implement in order for these methods to be referenced as interface instances. interface cannot be instantiated. Classes can implement multiple interfaces and are indexed through the interfaces of these implementations. An interface variable can only index an instance of the class that implements the interface.

Description
1, the interfaces in C # are defined independently of the class. This is opposed to the C + + model, where interfaces are actually abstract base classes.
2. Interfaces and classes can inherit multiple interfaces.
3, and the class can inherit a base class, the interface cannot inherit the class at all. This model avoids multiple inheritance problems in C + +, and implementations in different base classes in C + + may conflict. Therefore, complex mechanisms such as virtual inheritance and explicit scopes are no longer needed. C # 's simplified interface model helps to accelerate application development.
4, an interface defines a reference type that has only abstract members. What an interface in C # actually does, there is only a method flag, but no code is executed at all. This implies that an interface cannot be instantiated, and only an object derived from that interface can be instantiated.
5. Interfaces can define methods, properties, and indexes. So, comparing a class, the interface is specific: When you define a class, you can derive from multiple interfaces, and you can derive from only one class.

(i) Defining interfaces:
Technically, an interface is a set of data structures that contain a function-type method. With this set of data structures, client code can invoke the functionality of the Component object.
The general form of defining an interface is:
[Attributes] [modifiers] interface identifier [: Base-list] {interface-body}[;]
Description
1, attributes (optional): additional definition of information.
2, modifiers (optional): Allowed modifiers have new and four access modifiers. Respectively are: new, public, protected, internal, private. The same modifier is not allowed to occur more than once in an interface definition, and the new modifier can only appear in nested interfaces, representing members of the same name that have inherited. The public, protected, internal, and private modifiers define the access rights to the interface.
3, indicators and events.
4, Identifier: interface name.
5, Base-list (optional): contains one or more explicit base interface list, the interface is separated by commas.
6, Interface-body: The definition of interface members.

(ii) Access interface:
The same is true of the invocation of an interface method and the rules that take access to an index indicator as in a class. If the name of the underlying member is the same as the high-level member inherited, the underlying member overwrites the top-level member with the same name. However, because the interface supports multiple inheritance, in multiple inheritance, if two parent interfaces contain members of the same name, this produces two of semantics (which is one of the reasons why C # removes multiple inheritance mechanisms for classes), which requires explicit definition.
In C #, all methods within an interface are by default public methods.
Interfaces can be implemented by classes. The identifier of the implemented interface appears in the base list of the class.
When the base list of a class contains both a base class and an interface, the base class appears first in the list.

(iii) interface implementation:
1. Explicitly implement Interface member
In order to implement an interface, a class can define an explicit interface member executor (EXPLICIT Interface membership implementations). An explicit interface member Executor can be a method, a property, an event, or a definition of an index indicator, which should be consistent with the full name of the member.
2. Inheritance Interface Implementation
interface is invariant, but that does not mean that the interface is no longer evolving. Similar to class inheritance, interfaces can be inherited and developed.
Note: interface inheritance differs from class inheritance, and first, class inheritance is not only a description of inheritance, but also an implementation inheritance, while interface inheritance is simply a description of inheritance. In other words, a derived class can inherit a method implementation of a base class, and the derived interface inherits only the member method description of the parent interface, not the implementation of the parent interface, and secondly, class inheritance in C # allows only single inheritance, but interface inheritance allows multiple inheritance, and a sub-interface can have more than one parent interface.
Interfaces can inherit from zero or more interfaces. When inheriting from multiple interfaces, use ":" followed by the inherited interface name, separated by "," between multiple interface names. An inherited interface should be accessible, such as inheriting from a private type or an interface of internal type, which is not allowed. Interfaces do not allow you to inherit directly or indirectly from yourself. Similar to the inheritance of a class, the inheritance of an interface forms a hierarchy between interfaces.
3. Re-Implement the interface
As we've already described, derived classes can overload the member methods that are already defined in the base class. Similar concepts are introduced into the implementation of class-interface implementations, called Interface re-implementation. A class that inherits the interface implementation can implement the interface again. This interface requirement appears in the base class list of the class definition. The implementation of the interface must also adhere strictly to the rules for the first implementation of the interface, and the derived interface mappings do not have any effect on the interface mappings established for the implementation of the interface.
4, Mapping interface
The class must provide a specific implementation for the members of all interfaces listed in the base class table. The implementation of locating an interface member in a class is called an interface mapping (interface mapping).
Mapping, mathematically representing one by one of corresponding functional relationships. Interface mapping is also the same meaning, interface through the class to implement, so for each member defined in the interface, should be corresponding to a member of the class to provide it with a concrete implementation.
The following conditions must be met between a member of a class and its mapped interface members:
1. If both A and B are member methods, the name, type, and parameter list of A and B (including the number of parameters and the type of each parameter) should be consistent.
2. If both A and B are attributes, the names and types of a and B should be the same, and the accessors for A and b are similar. But if a is not an explicit interface member executor, a allows you to add your own accessors.
3, if A and B are time then the names and types of a and B should be the same.
4. If both A and B are index indicators, the type of a and B, the formal parameter list (including the number of parameters and the type of each parameter) should be consistent. And the accessors for A and b are similar. But if a is not an explicit interface member executor, a allows you to add your own accessors.
So, for an interface member, how do you determine which class member is going to implement it? That is, an interface member maps the members of which class. Here, we describe the process of interface mapping. Suppose Class C implements an interface Iinterface,member is a member of an interface IInterface, in which the mapping process is performed by WHO implements the interface member, that is:
1. If there is an explicit interface member executing body in C, which corresponds to the interface IInterface and its member members, it implements member members.
2. If the condition (1) is dissatisfied enough, and there is a non-static public member in C, which corresponds to the member member of the interface, the member is implemented by it.
3, if the above conditions are still not satisfied, then in Class C definition of the base class list to find a C base class D, using D instead of C.
4, repeat steps 1--3, traverse c all the direct base class and the indirect base class until you find a member of the class that satisfies the condition.
5, if still not found, then report an error.
In the interface mapping, also note the following two points:
1. When deciding which member of the class to implement an interface member, the interface member that is explicitly described in the class is implemented more preferentially than the other members.
2. Members that use private, protected, and static modifiers cannot participate in implementing interface mappings.

Reference site: http://blog.csdn.net/lyf1840/archive/2005/01/22/263467.aspx

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.