求助,匿名內(nèi)部類問(wèn)題,謝謝!
package?HM; interface?inter{ public?abstract?void?sleep(); } public?class?HelloWorld?{ public?static?void?main(String[]?args){ ff(new?inter(){//使用匿名內(nèi)部類創(chuàng)建接口的對(duì)象 public?void?sleep(){ System.out.println("睡覺(jué)"); } public?void?eat(){ System.out.println("吃飯"); } }); } public?static?void?ff(inter?in){ in.sleep(); } }
請(qǐng)問(wèn),如果我想調(diào)用匿名內(nèi)部類中的.eat方法,該如何操作?
2016-12-08
在接口中定義eat抽象方法,再在ff中調(diào)用in.eat()
2016-12-11
package com.imooc;
interface inter{
? ? public abstract void sleep();
? ? public abstract void eat();
}
public class HelloWorld {
public static void main(String[] args){
? ? ff(new inter(){//使用匿名內(nèi)部類創(chuàng)建接口的對(duì)象
? ? ? ? public void sleep(){
? ? ? ? ? ? System.out.println("睡覺(jué)");
? ? ? ? }
? ? ? ? public void eat(){
? ? ? ? ? ? System.out.println("吃飯");
? ? ? ? }
? ? });
? ? ?
? ? }
public static void ff(inter in){
? ? in.sleep();
? ? in.eat();
? ?
}
}