Java interface Syntax Details

Source: Internet
Author: User

Java interface Syntax Details

1, the default interface
In Java, you can use interface to define the behavior appearance of abstractions, such as methods in an interface that can be declared public abstract. For example:

public interface Swimmer{    public abstract void swim();}
    • 1
    • 2
    • 3

When the method in the interface is not operational, it must be open and abstract, and you can omit public abstract for convenience.

public interface Swimmer{    void swim();}
    • 1
    • 2
    • 3

Swim () is public abstract by default.
The compiler will automatically add public abstract to your application. Since the default must be public, therefore:

interface action{void execute (); class some implements Action{void execute () {System.out.printf ( "GGGGGG");}} public class main{public static void main (string[] args) {Action action=new Some (); Action.execute ();}}   
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

"What are the results of the implementation?" "The problem itself is a trap and cannot be compiled successfully because the execute () defined in action is actually default to public abstract, and the some class does not write public when the Execute () method is manipulated, so it is the default for package permissions, This then reduces the method of public in action to the package permissions, so the compilation fails. Execute () of the Some class must be set to public to compile.
Starting with JDK8, the methods in interface can be manipulated in a limited way, which is a new feature that is expanded to support lambda.
In interface, you can define constants. For example:

Package Hello;PublicInterfaceAction {Publicstatic final int STOP=0; public static final int right=0; public static final int left=0; public static final int up=0; public static final int down=0;}          
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

Such constants are called enumeration constants, which make the program look clear:

PublicInterfaceAction {PublicStaticFinalint stop=0;PublicStaticFinalint right=1;PublicStaticFinalint left=2;PublicStaticFinalint up=3; public static final int down=4"} class Game {public static void main (string[] args) {} public static void  Play (int action) {switch (action) { case Action. STOP:out.println ( "stop animation"); break; case Action. DOWN:out.println ( "play Down"); break, ...} }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25

This is much clearer than using switch to judge 0, 1, 2, 3, 4. Only enumeration constants can be defined in interface, so:

int STOP=0;int RIGHT=1;int LEFT=2;int UP=3;int DOWN=4;
    • 1
    • 2
    • 3
    • 4
    • 5

The compiler will expand automatically. Of course, defining enumeration constants in a class is also possible, but not abbreviated.
New enum syntax definition enumeration constants after JDK5:

public enum Action{    STOP,UP,DOWN,LEFT,RIGHT}
    • 1
    • 2
    • 3

Decompile can see that the action of the enum definition of the example is actually a class, and
The stop, right, lept, up, and down constants enumerated in the enum are actually public static final, and as an action instance, you cannot compose the program to instantiate the action directly because the constructor permission is set to private, only The action class can only be instantiated. Then you can use this action to declare the type. For example:

    public static void play(Action action) {        switch(action) { case Action .STOP: out.println("停止播放动画"); break; case Action .DOWN: out.println("向下播放"); break; ........ } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

public static void Play (Action action)
In this example, the action argument in the play () method is declared as the action type, so only the action instance is accepted, that is, only action.stop, action. Right, Action.left, Action.up, Action.down can be passed in. Unlike the Just Play () method, you can pass in any int value, and case comparisons can only enumerate action instances, so instead of the previous example, you must use default to check the execution period, and the compiler will perform type checking at compile time.

The class can manipulate more than two interfaces, if the same method is defined in two interfaces, and if the representation is the same method, it can be promoted to the parent interface, which inherits the interface again:

public Span class= "Hljs-class" >interface swimmer {void A ();} public interface action1 extends Swimmer {void B ();} public interface action2 extends Swimmer{void C ();              
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

Anonymous inner class
When writing Java programs, there is often a need to temporarily inherit a class or manipulate an interface and establish an instance. Because such subclasses or interface manipulation classes are used only once and do not need to define names for these classes, you can use the anonymous inner class (Anonymous Inner Class) to address this requirement. The syntax for an anonymous inner class is:

new 父类()|接口(){//类本体操作}Object obj=new Object() {            @Override            public String toString() { return "返利"; } };
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

Inherit object to redefine the ToString method. If an operation interface such as the Some interface defines an abstract method Dosome establishes an anonymous inner class:

new Some(){    public void doSome(){        //执行语句 };};
    • 1
    • 2
    • 3
    • 4
    • 5

JDK8 after the interface has only one method, can be created like this:

some = () ->{    //执行语句(Lambda)}

Java interface Syntax Details

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.