Explain the role of Java interfaces and may contain content

0 Introduction

Due to the epidemic, the time to be at home a little longer, a little learning can begin for a while, but recently really could not stand up, all kinds of inexplicable irritability, you can not learn anymore, worry ah, what does not do not want to learn, I really can not be saved. That's review previously learned, not exposure to new things is not a province began to dry up, take the opportunity to reinforce what basis. Well, so much nonsense on the pull, began following our protagonist interface.

1, the interface in the end is doing, and why you need an interface

We thought about a problem is not that we charge the phone, you can just head a charger and a data cable can be, where one can charge. Whether you are at home or at school or railway station also, you can find a charging socket on hate. That is why, they are good to discuss it, there is a distance between the two outlet ports and two ports also are the same. The answer is no, you think about it, there are so many manufacturers do produce socket, you want them to discuss this matter, it would not have produced on this subject can be discussed to die of old age. This time we need an authoritative body to stand up and tell everybody, do not discuss, I specify a manufacturer specifications you want to produce socket, we must implement all of the requirements in this specification, as this is not required on the basis of need in adding new content you decide yourself.
Our Java Interface is this specification, which requires that you must implement a given requirement (to correspond to Java in the abstract interface methods). Interface is doing, and it is clear, that is, it requires certain basic functions you need to achieve in the production of a thing when the basic requirements and implementation (such as the distance between the two sockets), why does the interface, He is afraid of you recklessly.

2, can have what internal interfaces

Different versions of JDK some of the content may be different, we are introduced according to different versions, pay attention to high version of something that contains low version, are added each version of content listed below.

version content
JDK 7 and older Members of the constants, the abstract method
JDK 8 The default method, a static method
JDK 9 and beyond Private methods

Each member in the interface definition:

  • Constant members
public static final int NUM = 10;  //常量名建议全部大写,初始化时必须赋值
  • Abstract method
public abstract void eat();
  • The default method
public default void drink(){
   
};
  • Static method
public static void house(){

}
  • There are four methods
private void pay(){

}

Some of the above modifiers can be omitted at the time of writing, the default compiler can only add this qualifier, which is why we omit the qualifier he will help us to automatically add, what specifically can be omitted. The figure is the front part of the gray
Here Insert Picture Description

2, usage and precautions

Here Insert Picture Description
Information on the methods used to achieve a static class call will surprise us given the wrong test to prove it:
Here Insert Picture Description

4, interfaces with the method of the parent class priority issues

If the interface method inherited from the parent class and we achieve the same signature, it will use the parent class, rather than the interface.

Published 141 original articles · won praise 131 · views 210 000 +

Guess you like

Origin blog.csdn.net/qq_41621362/article/details/105343872