Java How does this interface code really work?

I compiled the code below using ideas provided by other participants and then changing a couple of containers. For life of me, I can't really figure it out. The reason for the code is that I wanted to pass the function as a parameter. Part of the code I don’t really understand:

doFunc(numbers, new IFunction() { 
    public void execute(Object o) { 
       Integer anInt = (Integer) o; 
       anInt++;
       System.out.println(anInt);
    } 
}); 

As far as I understand, we use an interface to represent a function using an object (I think?). This is the full code:

public static void main(String[] args) {
    Integer[] strArray = new Integer[]{1,2,3,4,5};

    List numbers = Arrays.asList(strArray);
    doFunc(numbers, new IFunction() { 
        public void execute(Object o) { 
           Integer anInt = (Integer) o; 
           anInt++;
           System.out.println(anInt);
        } 
    }); 
    for(int y =0; y<numbers.size();y++){
        System.out.println(numbers.get(y));
    }
}

public static void doFunc(List c, IFunction f) { 
   for (Object o : c) { 
      f.execute(o); 
   } 
}

public interface IFunction { 
    public void execute(Object o); 
} 

I guess I just need someone to go a little slower explaining this. Thank you for agreeing with him.

+3
source share
3 answers

, , , doFunc, , - . :

public interface IFunction { 
    public void execute(Object o); 
}

, , IFunction, execute, .

public static void doFunc(List c, IFunction f) { 
   for (Object o : c) { 
      f.execute(o); 
   } 
}

c , IFunction, execute, IFunction, c.

    doFunc(numbers, new IFunction() { 
        public void execute(Object o) { 
           Integer anInt = (Integer) o; 
           anInt++;
           System.out.println(anInt);
        } 
    });

main , IFunction. - , - , execute, inline.

, IFunction, doFunc, - throwaway, , .

+1

. :

public static void main(String[] args) {
    Integer[] strArray = new Integer[]{1,2,3,4,5};

    List numbers = Arrays.asList(strArray);
    doFunc(numbers, new ConcreteFunction()); 
    for(int y =0; y<numbers.size();y++){
        System.out.println(numbers.get(y));
    }
}

public static void doFunc(List c, IFunction f) { 
   for (Object o : c) { 
      f.execute(o); 
   } 
}

public interface IFunction { 
    public void execute(Object o); 
} 

public class ConcreteFunction implements IFunction {
    public void execute(Object o) { 
       Integer anInt = (Integer) o; 
       anInt++;
       System.out.println(anInt);
    } 
}

, , .

. :

+5

IFunction , , .

public void execute(Object o);

This means that any object that is an IFunction has this method. The anonymous IFunction defined in your example translates the argument to an integer, and then increments it and prints the value.

Since doFunc requires a List of objects and an object that implements IFunction, a call in the main passages numbers, a list of numbers and an anonymous IFunction, which increments them and displays their value.

doFunc then takes these objects in a list and passes them as an argument to IFunction f.

0
source

Source: https://habr.com/ru/post/1785467/


All Articles