By understanding the differences between list and ilist, We can deepen our understanding of interface callback.

Source: Internet
Author: User
List is a class and ilist is an interface. Because list inherits ilist during design, so in Microsoft's pet shop4.0, variables at the Module layer are declared in the following form (which helps to improve flexibility and polymorphism ).

Ilist < Module > Module =   New List < Module > ();

This made OOP beginners like me confused and gave rise to some silly ideas: Is it true that list and ilist can be used in declaration? Are they the same thing?

This is an unclear basic concept and does not understand the relationships, differences and functions between interfaces and classes.

Next, let me give an example:

Ilist < String > AA =   New List < String > ();
List < String > Bb =   New List < String > ();
AA = BB; // Pass
Bb = AA; // Fail

Why? Because list inherits the ilist interface, AA = BB can be passed, but not vice versa.

Then someone asked: Even if aa = BB is available, I will use it all later. Ilist < String > AA = New List < String > (); Is not required. List < String > Bb = New List < String > (); Is that all right?

If you have questions about this line, let's look at another example:

Class Dog: ieat
{
Public   Void Doeat ()
{
// Food
}
}

InterfaceIeat
{
VoidDoeat ();
}

The above defines an eat interface. The dog class inherits this interface and implements its doeat method. Next let's declare it.

// Use class declaration directly
DOG d =   New Dog ();
D. doeat ();

//Interface Declaration
Ieat D2= NewDog ();
D2.doeat ();

At this time, D and D2 play exactly the same role (although the two concepts are different, they are used exactly the same). If you think it is where you are confused, let's modify the example so that you can see their differences.

Class Dog: ieat
{
Public   Void Doeat ()
{
// Food
}

Public VoidTail ()
{
//Tail shake
}
}

InterfaceIeat
{
VoidDoeat ();
}

 

AboveCodeIn the Dog class, we add a new action tail. In this case, the previous D and D2 are different. tail (); is usable, while d2.tail (); is unavailable because there is no tail method in the ieat interface.

Now that ieat declaration can use less methods than the Declaration Dog class, what is the use of the ieat interface for declaration? To solve this problem, let's modify the code:

Class Dog: ieat
{
Public   Void Doeat ()
{
// Dog food
}

Public VoidTail ()
{
//Tail shake
}
}

class Human: ieat
{< br> Public void doeat ()
{< br> /// people eat
}

Public VoidWork ()
{
//Work
}
}

InterfaceIeat
{
VoidDoeat ();
}

We added a new human class that also inherits from the ieat interface. We found that the ieat interface can be instantiated by the dog class or the human class:

// Instantiate ieat with the dog class
Ieat E =   New Dog ();
E. doeat ();

//Instantiate ieat with the human class
Ieat E2= NewHuman ();
E2.doeat ();

Although e and E2 in the above Code are both ieat types, they do different things. This is a manifestation of polymorphism in OOP, in reality, we don't really declare and instantiate two or more interfaces directly like the above Code, but write a method to implement multi-state calling (simple factory ).

Static   Void Main ( String [] ARGs)
{
Eat ( New Dog ()); // Call dog
Eat ( New Human ()); // Call human
}< P>

private void eat (ieat e)
{< br> E. doeat ();
}

we only need to call the Eat method, different functions are implemented based on the input instance type.

we introduced the difference between ilist and list, and introduced some experiences of interface callback learning this afternoon, which may be very simple, however, it is not easy to understand and leave a mark for yourself.

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.