An interesting java interface and an interesting java Interface
The functions of interfaces are not easy to understand for many new users. I will give you an example.
The interface is just a specification, so the methods in it are empty.
If I open a pet grain store and declare that all pets can come to buy food here, this is equivalent to an interface,
Public interface PetRestaurant {
Public void buy ();
}
When a dog sees it and knows that he is a pet, it implements this interface.
Public class DogPet implements PetRestaurant {
@ Override
Public void buy (){
System. out. println ("I'm a dog, I want to buy dog food ");
}
}
When a cat sees it, he knows that he is also a pet, so he also implements this interface.
Public class CatPet implements PetRestaurant {
@ Override
Public void buy (){
System. out. println ("I'm a cat, I want to buy cat food ");
}
}
Before Dogs and Cats came to my shop, I didn't know what they really were, but when they came to my shop, I knew they wanted cat food and dog food. Because they all implement this interface, you can buy it. The following class is equivalent to a customer reception class, that is, the shop assistant. He receives all the animals that implement the interface of my pet shop and transmits them to a pet of the PetRestaurant type. Note that this PetRestaurant is an interface.
Public class test {
Public void buy (PetRestaurant pet)
{
Pet. buy ();
}
}
Okay, this time my boss appears. I can sell them food, which is equivalent to testing.
Public class Tests {
Public static void main (String [] args ){
PetRestaurant dog = new DogPet (); // instantiate a dog, which is equivalent to instantiating a dog customer
PetRestaurant cat = new CatPet (); // instantiate a cat, which is equivalent to instantiating a cat customer
Test t = new test (); // instantiate a store Administrator
T. buy (cat); // give the cat to the store administrator.
T. buy (dog); // give the dog to the store administrator.
}
}
The result is
I'm a cat. I want to buy cat food.
I'm a dog. I want to buy a bitch.
You know, throughout the whole process, I don't know whether it's a cat, a dog, or anything else. If I have a store employee, I handed over all the unknown animals to the store staff, who knew how to sell them, because these dogs and cats all implemented the interfaces of my pet shop, the store Administrator is responsible for receiving all the animals that have implemented this interface. This is a benefit. If I have a pig tomorrow, as long as it implements my interface, I just handed it over to the store staff for processing. I don't need to change this shop staff, the shopkeeper just needs to instantiate this animal.
You think, if there is no interface, what should you do? Come to a cat, I want to create a cat, and instantiate it. Come to a dog, I want to create a dog, and I also want to instantiate it, it would be quite troublesome to have a dedicated store to receive the reception.
Author: meiyunluo
Link: http://www.zhihu.com/question/20111251/answer/14760353
Source: zhihu
Copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.