Can a lambda access members of its target functional interface?

后端 未结 3 627
独厮守ぢ
独厮守ぢ 2020-12-16 13:56

I have created a simple interface using java8. In that it contains one method and one default method.

interface Lambda{

default void dummy(){
    System.out         


        
3条回答
  •  情书的邮戳
    2020-12-16 14:25

    If I understand it correctly. You are trying to call a default method in an interface through lambda implementation. I think it can be done.

    @FunctionalInterface
    interface Value 
    {
        String init(Value a);
    
    
        default String add(String b) 
        {
            return "added content "+b;
        }
    
    
        default String getResult()
        {
            String c = init(this);
            return c;
        }
    }
    
    public class Main
    {
    
    
        public static void main(String[] args)
        {
    
            Value v = a -> a.add("inpout"); // here I am calling add method in Value interface.
            String c = v.getResult();
    
            System.out.println(c);
    
        }
    }
    

提交回复
热议问题