简体   繁体   English

Java这个接口代码实际上如何工作?

[英]Java How does this interface code actually work?

I've put together the code below using ideas giving to me by fellow members and then changing a couple of the containers. 我已经使用其他成员给我的想法然后更改了几个容器来汇总下面的代码。 For the life of me i cant really get my head around some of this. 对于我的生活,我真的无法理解其中的一些。 The reason for the code is that i wished to pass a function as a parameter. 代码的原因是我希望将函数作为参数传递。 The part of code i especially don't understand is: 我特别不理解的代码部分是:

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

I understand to some extent that we're using 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 it. 我想我只需要有人慢一点解释它。 Thanks for bearing with me. 谢谢你的支持。

That's an anonymous inner class. 这是一个匿名的内部阶级。 You could do as good as follows: 你可以做到如下:

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);
    } 
}

The difference is that a concrete class is reuseable while an anonymous inner class is not. 不同之处在于具体类是可重用的,而匿名内部类则不可重用。

See also: 也可以看看:

The main concept here is that since the second object you're passing to doFunc is anonymous , you don't need to instantiate an object here - just the interface. 这里的主要概念是,由于你传递给doFunc的第二个对象是匿名的 ,你不需要在这里实例化一个对象 - 只是接口。 Here's what each part of the code is saying: 以下是代码的每个部分所说的内容:

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

This says that any object which implements the interface IFunction has one method, execute , which it runs on another Object. 这表示实现接口IFunction任何对象都有一个方法execute ,它在另一个Object上运行。

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

This function takes a List c and any Object which implements IFunction , then runs the execute method - guaranteed to be in the second object by the IFunction interface - over all the objects in c . 此函数接受List c任何实现IFunction的Object ,然后运行execute方法 - 保证在IFunction接口的第二个对象中 - 在c所有对象上。

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

This snippet from main takes a list of numbers and creates an anonymous object in-place which implements the IFunction interface. main这个片段获取一个数字列表,并在就地创建一个实现IFunction接口的匿名对象 Since it's not any concrete object type, it doesn't need to have any other methods, just execute , which it defines inline. 由于它不是任何具体的对象类型,因此它不需要任何其他方法,只需execute ,它定义内联。

The end result is that your IFunction object declared inside the call to doFunc is effectively a functor - it's a throwaway object that encapsulates a function, which can be run over a list of objects. 最终结果是在doFunc调用中声明的IFunction对象doFunc是一个doFunc 函数 - 它是一个一次性对象,它封装了一个可以在对象列表上运行的函数。

IFunction, in this case, specifies an interface that anyone who implements the interface must define. 在这种情况下,IFunction指定实现接口的任何人必须定义的接口。

public void execute(Object o);

This means that any object that is an IFunction has this method. 这意味着任何作为IFunction的对象都具有此方法。 The anonymous IFunction being defined in your example casts it's argument to an integer and then increments it and prints the value. 在您的示例中定义的匿名IFunction将其参数转换为整数,然后递增它并打印该值。

As doFunc requires a List of objects and an object that implements IFunction, the call in main passes numbers , a list of numbers, and the anonymous IFunction which increments them and prints their value. 由于doFunc需要一个对象List和一个实现IFunction的对象,main中的调用传递numbers ,一个数字列表和匿名IFunction,它们递增它们并打印它们的值。

doFunc then takes these objects in the list and passes them as an argument to the IFunction f. 然后,doFunc将这些对象放在列表中,并将它们作为参数传递给IFunction f。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM