Meyunro
Links: https://www.zhihu.com/question/20111251/answer/14760353
Source: Know
Copyright belongs to the author, please contact the author for authorization.
The interface is just a specification, so the methods inside are empty.
If I open a pet Liang Dian, declare that all pets can come to me to buy food, which is equivalent to an interface,
Public interface Petrestaurant {
public void Buy ();
}
When a dog sees, knows that he is a pet, so it goes to implement this interface
public class Dogpet implements Petrestaurant {
@Override
public void Buy () {
System.out.println ("I am a dog, I want to buy dog food");
}
}
When a cat sees and knows that he is a pet, it also implements this interface.
public class Catpet implements Petrestaurant {
@Override
public void Buy () {
System.out.println ("I am a cat, I want to buy cat food");
}
}
When dogs and cats come to my shop, I don't know exactly what they are, but when they come to my shop, I know one to cat food, one to dog food. Because they all realized I this interface, can buy. The following class is equivalent to a customer-friendly class, bartender, who receives all the animals that have implemented the interface of my Pet shop, passing in a petrestaurant type of pet, note that this petrestaurant is an interface
public class Test {
public void Buy (Petrestaurant pet)
{
Pet.buy ();
}
}
Well, this time my boss showed up and I could sell them food, equivalent to the test class
public class Tests {
public static void Main (string[] args) {
Petrestaurant dog = new Dogpet (); Instantiating a dog is the equivalent of instantiating a dog customer
Petrestaurant cat = new Catpet ();//Instantiate a cat, the equivalent of a cat customer instantiation
Test T = new Test (); Instantiate a bartender
T.buy (CAT); Give the cat to bartender.
T.buy (dog); Give the dog to bartender.
}
}
The result of this operation is
I'm a cat, I want to buy cat food
I'm a dog, I want to buy a dog.
You know, the whole process I this shopkeeper actually do not know whether the cat is a dog or anything else, I just have a bartender, these come to the unknown what animals are all to bartender, bartender know how to sell, because these dogs ah cats have realized my Pet shop interface, and bartender is responsible for receiving all the animals that have realized my interface. This has a benefit, if tomorrow comes a pig, as long as it realizes my this interface, I just hand over to bartender processing OK, I this shop small two do not need to change, I this shopkeeper also only need to instantiate this animal on OK
You want to, if there is no interface, what will do, to a cat, I'm going to create a cat, but also to instantiate, to a dog, I want to create a dog, the same to instantiate, but also equipped with special bartender to receive, will be quite troublesome ================================== Dion
Links: https://www.zhihu.com/question/20111251/answer/14012223
Source: Know
Copyright belongs to the author, please contact the author for authorization.
“An interface is a specification"That's right.
“It would be easier to write the implementation method directly in this class."And how do you guarantee thisInterface on a class to achieve it? If more than one class implements the same interface, how does the program know that they are related?
Since it is not a class to achieve, it is that there are many places to use, and we need to unify the standards.even some programming languages (OBJECT-C) have not called the interface interface, directly called protocol.
The purpose of uniform standards is that everyone knows this isdo what, but don't know the specificsWhat to do。
For example:
I know comparable this interface is used to compare two objects, so how to compare?
Numbers have numeric comparisons, strings have a comparison of strings, and students (their own classes) have their own methods of comparison.
Then, in another code that is responsible for the ordering of objects (not necessarily numbers), you must compare two objects.
What are the types of these two objects?
Object A, B? Certainly not, a > B syntax like this cannot be compiled.
int A, b? Or not? In the beginning, it's not necessarily a number.
....
So, comparable is coming. He tells the compiler that a B two objects all satisfy the comparable interface, that is, they can be compared. Specifically, this procedure does not need to be known.
So, he needs some concrete implementations, the comparable interface has a method called CompareTo. Then this method is used to replace operators such as <, >.
This is because the operator is reserved for comparison with built-in types (integers, floating-point numbers) rather than a generalized comparison operation.
If you can understand an existing interface such as comparable in the JDK's own library, it is easy to understand why you need to use the interface when developing the program.
Understanding Java Interfaces