我傳遞一個接口作爲匿名執行一個不同的對象是這樣的:傳遞匿名類
public interface Interface {
public int convert (int a);
}
public static void main(String[] args) throws IOException, InterruptedException {
final int[] array = {1,6,3,5,7,8,4,0,3};
Interface inter = new Interface() {
public int convert(int a) {
int result = a;
for (int i = 0; i < array.length; i++) {
a=a+array[i];
}
return a;
}
};
SomeObject ty = new SomeObject();
ty.Test(7, inter);
}
public class SomeObject {
public void Test(int number, Interface inter) {
System.out.println(inter.convert(number));
}
}
我的問題是:它是如何工作的? SomeObject
如何知道未直接傳遞給對象的數組(數組不是匿名類的成員)。
更新
(對不起,遲到更新)
什麼有關成員瓦爾或在匿名類使用的方法的方法呢?他們不是最終
Interface inter = new Interface() {
public int convert(int a) {
int result = a + someMemberVar;
for (int i = 0; i < array.length; i++) {
a=a+array[i];
}
return a;
}
};
行'int result = a;'的目的是什麼? – 2011-12-20 15:31:31