java——匿名内部类2:

2019年2月16日16:33:41 发表评论 922 views
interface A
{
    public void fun1();
}
class B
{
    int i = 10;
    class C implements A
    {
        public void fun1()
        {
            System.out.println(i);
        }
    }
    public void get(A a)//用于A接口的实例化并调用fun1()方法
    {
        a.fun1();
    }
    public void test()//此方法用于调用get()方法
    {
        this.get(new C());
    }
}






/**
 * Created by xabcd on 2019/2/16.
*/
public class test_niming
{
    public static void main(String[] args)
    {
        B b = new B ();
        b.test();
    }
}




结果:
10

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: