作者awert ( )
看板java
標題Re: [問題] 能否在迴圈裡面呼叫 anonymous class 的 method?
時間Tue Jul 24 23:22:09 2012
雖然不是 anonymous class,不過至少有達到你要的需求
public class Person {
public static void main(String[] args) {
class John extends Person {
protected void greeting() {
talk();
System.out.println("My Name is John");
}
}
John j = new John();
for (int i = 0; i < 5; i++) {
j.greeting();
}
}
protected void talk() {
System.out.println("Hello");
}
}
output :
Hello
My Name is John
Hello
My Name is John
Hello
My Name is John
Hello
My Name is John
Hello
My Name is John
--
We who cut mere stones must always be envisioning cathedrals.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.35.186.241
推 LaPass:protected void greeting() <= public 07/24 23:50
→ awert:inner class連private member都可以被access,不需用到public 07/25 00:44
推 LaPass:對喔..... XD 07/25 08:08
推 GALINE:不過這是我要拿的東西根本就在我自己的 class 轄下才能用 07/25 09:50
→ GALINE:但這種情況我其實就直接呼叫就好了[汗] 07/25 09:52
推 GALINE:是說我要做的事本來是不應該可以做的,只是想說既然 07/25 09:55
→ GALINE:在指定給某個之前變數前能用的話,會不會有其他洞可鑽... 07/25 09:56
→ GALINE:之前變數 -> 變數之前 ....我腦袋變 FILO 了 =__= 07/25 09:58