Java这个接口代码实际上是如何工作的?
我已经把下面的代码放在一起,使用同事们给我的想法,然后更改几个容器。就我的一生而言,我真的无法理解这些。编写代码的原因是我希望将函数作为参数传递。我特别不理解的代码部分是:Java这个接口代码实际上是如何工作的?,java,Java,我已经把下面的代码放在一起,使用同事们给我的想法,然后更改几个容器。就我的一生而言,我真的无法理解这些。编写代码的原因是我希望将函数作为参数传递。我特别不理解的代码部分是: doFunc(numbers, new IFunction() { public void execute(Object o) { Integer anInt = (Integer) o; anInt++; System.out.println(anInt);
doFunc(numbers, new IFunction() {
public void execute(Object o) {
Integer anInt = (Integer) o;
anInt++;
System.out.println(anInt);
}
});
我在某种程度上理解,我们使用接口来表示使用对象的函数(我想是吧?)。这是完整的代码:
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);
}
publicstaticvoidmain(字符串[]args){
整数[]strArray=新整数[]{1,2,3,4,5};
列表编号=数组.asList(strArray);
doFunc(数字,新的iffunction(){
公共无效执行(对象o){
整数anInt=(整数)o;
anInt++;
系统输出打印ln(anInt);
}
});
对于(int y=0;y,这是一个匿名的内部类。您可以做如下工作:
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);
}
}
publicstaticvoidmain(字符串[]args){
整数[]strArray=新整数[]{1,2,3,4,5};
列表编号=数组.asList(strArray);
doFunc(数字,新的具体函数());
在本例中,for(int y=0;yIFunction)指定任何实现接口的人都必须定义的接口
public void execute(对象o);
这意味着任何作为IFunction的对象都有此方法。示例中定义的匿名IFunction将其参数转换为整数,然后递增并打印值
由于doFunc需要一个对象列表和一个实现IFunction的对象,因此main中的调用传递数字
、一个数字列表和一个匿名IFunction,后者递增这些数字并打印其值
doFunc然后在列表中获取这些对象,并将它们作为参数传递给IFunction f。这里的主要概念是,由于传递给doFunc
的第二个对象是匿名的,因此您不需要在这里实例化一个对象,只需实例化接口。下面是代码的每个部分所说的内容:
public interface IFunction {
public void execute(Object o);
}
这表示实现接口iffunction
的任何对象都有一个方法execute
,它在另一个对象上运行
public static void doFunc(List c, IFunction f) {
for (Object o : c) {
f.execute(o);
}
}
此函数获取一个列表c
和实现IFunction的任何对象,然后在c
中的所有对象上运行execute
方法,IFunction接口保证该方法位于第二个对象中
doFunc(numbers, new IFunction() {
public void execute(Object o) {
Integer anInt = (Integer) o;
anInt++;
System.out.println(anInt);
}
});
来自main
的这段代码获取了一个数字列表,并在适当的位置创建了一个匿名对象,该对象实现了IFunction接口。由于它不是任何具体的对象类型,因此它不需要任何其他方法,只需执行它内联定义的execute
最终的结果是,在调用doFunc
中声明的IFunction对象实际上是一个函子-它是一个一次性对象,封装了一个函数,可以在对象列表上运行。我建议您尝试在调试器中单步执行代码,以查看每行代码的功能。使用泛型可以实现这一点代码更清晰IMHO.我认为当你说“实现接口IFunction的任何对象都有一个方法”时,我们在这里不使用关键字“implements”-那么当我们创建“new”对象IFunction时,默认实现是否完成了?正确-因为你将对象声明为new IFunction()
,实现
是隐式的。试试看:如果您使用的是Eclipse或其他类似的IDE,请在新IFunction(){…}
块中注释掉执行
的定义,您的IDE应该对您大喊大叫“没有为接口IFunction中的所有函数提供实现”