java——匿名内部类2:

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

留下评论

您的邮箱地址不会被公开。 必填项已用 * 标注