:: (double colon) operator in Java 8

前端 未结 17 2830
旧时难觅i
旧时难觅i 2020-11-21 11:10

I was exploring the Java 8 source and found this particular part of code very surprising:

//defined in IntPipeline.java
@Override
public fin         


        
17条回答
  •  长发绾君心
    2020-11-21 11:58

    In older Java versions, instead of "::" or lambd, you can use:

    public interface Action {
        void execute();
    }
    
    public class ActionImpl implements Action {
    
        @Override
        public void execute() {
            System.out.println("execute with ActionImpl");
        }
    
    }
    
    public static void main(String[] args) {
        Action action = new Action() {
            @Override
            public void execute() {
                System.out.println("execute with anonymous class");
            }
        };
        action.execute();
    
        //or
    
        Action actionImpl = new ActionImpl();
        actionImpl.execute();
    }
    

    Or passing to the method:

    public static void doSomething(Action action) {
        action.execute();
    }
    

提交回复
热议问题