The role of interfaces in Java

Source: Internet
Author: User

Said small town opened a pet food store, small animals can go to buy snacks to eat, we are very happy, cat ah, dog ah, pig ah ... We're all about to go together!

/*

* This is a dog class, because the doorway does not write specifications, they do not know what they want to say

* */

public class Dogpet {

}



/*

* This is a cat class, they don't know what they're going to say because they don't have a code at the door.

* */

public class Catpet {

}



/*

* This is a pig class, because there is no specification, they do not know what to say

* */

public class Pigpet {

}



/*

* Shop Small Two, his main work is to sell food, how to sell it? Well, take a closer look at what animals come in, and then sell them snacks.

* */

public class Waiter {

If it's a cat, it sells small fish.

public void Sell (Catpet cat) {

System.out.println ("You are a cat, give you little Fish");

}

If it's a dog, it sells bones.

public void Sell (Dogpet dog) {

System.out.println ("You are Wang Choi, give you Bones");

}

If it's a pig, sell the vegetables.

public void Sell (Pigpet pig) {

System.out.println ("You are a grumble, give you vegetables");

}

}

/*

* If there are 100 kinds of small animals in the town, the shop small two to learn to recognize 100 times, and to remember what they want to buy, so tired oh ...

* */

/*

* The animals are coming to the store, ready to start shopping.

* */

public class shopping{

public static void Main (String args[]) {

Waiter waiter = Newwaiter ();//There is a bartender

Catpet cat = Newcatpet (); Here comes a mew meow.

Dogpet dog = Newdogpet (); Here comes a bark.

Pigpet pig = Newpigpet (); Here comes a pig.

The deal started, bartender, judging by the animals.

Waiter.sell (cat);//bartender picked up the cat, and lo and behold, oh, this is the cat, it is to eat fish

Waiter.sell (dog);//Bartender Pick up the dog, look, oh, this is the dog, it is to eat bones

Waiter.sell (pig);//Bartender Pick up the pig, look, oh, this is a pig, it is to eat vegetables

}

}

/*======================

The output results are as follows ******

You're a cat, and you're a dried fish.

You're Wang Choi, you got a bone.

You are a lulu, give you vegetables

*

* Although the transaction can be completed, but if there is a small rabbit, but shop small two do not know how to do? Does that have to be checked? So you have to modify the bartender method to add a rabbit method.

* And if there are 100 small animals, does it have to be modified 100 times? From this can be seen, the object more, bartender work pressure is very big ah.

*======================

* */

-----------------------------------------------Story Split Line------------------------------------------------

Finally, the small animals more and more, bartender more and more tired, then once sold wrong, was criticized, so do not do, the boss worried Ah, think of a way, in the doorway erected a block sign, write: Into the point of the small animals to say what to eat! or not to sell! This is like an interface, is a specification: The animals must follow its requirements to do, say the name of the snack. As for what to say, that is the small animals things ...

/*

* The door set a rule: we want to buy snacks, you have to say, that is, to achieve the Buy () method.

* */

Public Interfacespeakout {

public void Speak ();

}

/*

* After the puppy saw it, he knew the new rules and realized the method

* */

public class Dogpetimplements speakout{

@Override

public void Speak () {

TODO Auto-generatedmethod stub

System.out.println ("I am a dog, want to buy bones!");

}

}



/*

* After the kitten saw it, he knew the new rules and realized the method

* */

public class Catpetimplements speakout{

@Override

public void Speak () {

TODO Auto-generatedmethod stub

System.out.println ("I am a cat, want to buy small fish!");

}

}



/*

* New Shop Small Two don't know what they want, but it doesn't matter, animals will say it.

* Without implementing an interface, of course, you can't sell it, because it doesn't know what to do, and bartender doesn't know what to sell.

* */

public class Waiter {

public void Sell (Speakout pet) {//does not comply with this requirement and does not sell, so the pass-in parameter must be specified.

Pet. Speak ();

}

}



/*

* Cats and dogs come to the store and start trading and can sell them food

* */

public class shopping{

public static void Main (String[]args) {

Dogpet dog = Newdogpet (); Instantiate object, come a dog customer

Catpet cat = Newcatpet (); Instantiate object, come a cat customer

Both of the instantiation methods are OK

Speakout dog = Newdogpet (); Instantiate object, come a dog customer

Speakout cat = Newcatpet (); Instantiate object, come a cat customer

Waiter waiter = Newwaiter (); Instantiate a bartender

Waiter.sell (CAT); Bartender began to sell things, only to pick up the cat, the cat will say what

Waiter.sell (dog); Pick up a dog, and the dog will say what he wants.

}

}

/*

* =========================

* Output Result:

* I'm a cat, I want to buy a little fish!

I am a dog, want to buy bone!

* The Java language is characterized by object-oriented, what does it mean? The story of the cat and dog are objects, to play the characteristics of the object, such as the story let the cat and dog themselves tell the store small two to what,

Instead of being judged by bartender according to the small animal, what to sell to it, the operation is given to the small animals.

* So if the piglet sees it, it just needs to be implemented to buy it in the store.

* Do not write all functions in the main function or call the function, so bartender is much easier.

* =========================

**/

The pig rushed over and didn't see the rules, so could you sell it? Certainly can not, want to buy, can ah, obey the stipulation, realize method, oneself say what to want.


This is the most popular interpretation of the interface, the role of the interface is unified norms, that is, let everyone know what this is to do, but how to do, do not know, by the implementation of their own to do.

The role of interfaces in Java

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.