C # fourth time job (interface)

Source: Internet
Author: User


{ Interface Body}

Various interpretations

attributes (optional): Additional defined information that allows complex numbers.

Access-modifiers (optional): The modifier that is allowed to be used has a new and four access modifiers. respectively:new, public,protected,internal, private. The same modifier is not allowed to appear more than once in an interface definition, and the new modifier can only appear in a nested interface, overwriting the inherited member with the same name. Public, protected, internal, private modifiers define access to the interface, and the default value is internal.

Interface-name: Interface name, does not need to be interpreted.

Base-inteface-list (optional): Contains a list of one or more explicit base interfaces, separated by commas between the interfaces.

Interface Body: Interface body (cannot have access modifier), just define the method and properties of the interface, etc., the actual implementation is written in the class using the interface.

How to use

The interface is used through the class inheritance interface, which is the interface body part of the class execution interface, and the class inherits the interface to execute all instructions.

Interfaces can inherit interfaces, using methods equivalent to class inheritance, and called after they are applied. Interface inheritance interfaces do not implement methods in an inherited interface, but when a class inherits an interface that inherits other interfaces, it is important to implement all the methods in all interfaces on the inheritance chain, or it will error.

A class can invoke multiple interfaces and execute in order.

Examples of interface sounds

public interface istorable{
int x{get;set;}\\ Storage Variable
}

Interface inheritance

The public class document:istorable//inherits the general class general inheritance interface, which is similar to C and C + + with similar operations {public    void Read () {...} Here you can add modifiers and write the implementation code public    void Write (Object obj) {...}    ...}

There are many forms of interface inheritance, but the same thing happens when you inherit the statements in the interface regardless of what operation is performed.

In general, try not to put in an interface a variety of different functions of the statement, easy to cause logical confusion, rather than establish multiple interfaces to use, convenient and not easy error.

Interface pollution is the most likely problem, that is, if a class A implements this interface B, but a "function" C in interface B is not possible at all, because a implements interface B, so must implement all the "function" in B, then this c is also to be a realization, obviously this c is a simply impossible to have.

That is, logic is confusing, so it is often better not to put a large number of different functions in a single interface. Use a thin interface as much as possible.

C # fourth time job (interface)

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.