Interface as return type (reprinted)

Source: Internet
Author: User

The returned type is an interface, but the interface cannot be instantiated, but the interface can declare a reference to its implementation class, that is, the return value in practice is an object of the implementation class of this interface. This is a major feature of object-oriented

I want to interface "I food chain top drop man", you return to "The Lord", what is this ghost? Can you understand?

You can say that this process didn't happen.
Just inside the method, you could be any object
But outside of the method, the object you get can only use the method provided by that interface, that's all.

The return type is a discussion of the interface

http://bbs.csdn.net/topics/380209722

public static ilist<int> A ()
{
B=new list<int> ();
return b;
}

Instead of returning a list, what does the IList interface mean?

=========================================================================

If you write a function, make sure that only one type is returned. Then, when choosing the return value type, the more accurate the better.
such as String string. Replace (String, String), which is more than an object string. Replace (String, String) is good.

If you know that you are returning a type, then it is not meaningful to use an interface to declare the variable when it is called. But in such a case the exception (I don't want to say is not rigorous by some people said, but it is true that the exception of the situation you are very difficult to meet): This class explicitly implements an interface, and you have to use this interface method, a scenario is that this class implemented two different interfaces, The two interfaces have two methods with the same method signature but different meanings, so the class explicitly implements the interface. At this point you have to specify the type of interface you are using instead of the return value type.

=================================================================================

The return interface is generally applied to the abstraction layer.

Ilist<t> can be understood as a woman.
List<t> can understand your girlfriend.
Your girlfriend can have a woman's attribute, but not everyone with a woman's attributes is your girlfriend.
And the toilet has a clear definition that fits the woman's definition and can go into the ladies ' room. The non-conforming cannot be entered. (There are, of course, bugs, such as cleaners that may be male.) )
Do you think it's meaningful to be precise with every specific woman?

=========================================================================

public static ilist<int> A ()
{
B=new list<int> ();
return b;
}

Here the return value type is: A ilist<t> generic interface, which is intended to make the returned B more widely used, and any class that inherits the Ilist<t> interface can use B without a type conflict

=================================================================

Method is the return interface or a concrete class, and interface-oriented programming

http://www.cnblogs.com/kirinboy/archive/2012/03/08/ Should-method-return-interface-or-concrete-class-and-programming-to-an-interface.html

Interface as return type (reprinted)

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.