码迷,mamicode.com
首页 > 编程语言 > 详细

Java内部类之匿名内部类

时间:2017-05-30 20:56:52      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:strong   read   cost   static   初始化   over   class   引用   ide   

??我们都知道Java中可以使用内部类,将一个类的定义放在另一个类的定义的内部,这就是内部类,但是匿名内部类往往使我们摸不着头脑,因为它并没有特定的名称,那么该如何使用它呢?

定义一个匿名内部类

public interface Contents
{
    int value();
}

public class Test1
{
    private Contents contents()
    {
        return new Contents()
        {
            private int i = 11;
            @Override
            public int value()
            {
                return i;
            }
        };
    }

    public static void main(String[] args)
    {
        Test1 test = new Test1();
        Contents con = test.contents();
        System.out.println(con.value());
    }
}

??如上,我们就定义了一个匿名内部类,它实现了Contents接口,通过new表达式返回的引用被自动向上转型为对Contents的引用,输出结果就是11。这是一个最基本的匿名内部类,只是输出了一个结果,下面我们来看看使用外部对象的匿名内部类。

使用带参数的匿名内部类

public interface Destination
{
    String readLabel();
}

public class Test2
{
    private Destination destination(final String dest)
    {
        return new Destination()
        {
            private String label = dest;
            @Override
            public String readLabel()
            {
                return label;
            }
        };
    }

    public static void main(String[] args)
    {
        Test2 test2 = new Test2();
        Destination d = test2.destination("Wu Han");
        System.out.println(d.readLabel());
    }
}

??上面的匿名内部类传入了一个参数,该匿名内部类使用了一个在外部定义的对象,那么编译器就要求其参数引用是final的。但是在JDK1.8中,就算没有设置为final也没有报错。

对匿名内部类初始化

public interface Destination
{
    String readLabel();
}

public class Test3
{
    public Destination destination(final String dest, final float price)
    {
        return new Destination()
        {
            private int cost;
            {
                cost = Math.round(price);
                if(cost > 100)
                {
                    System.out.println("超出预支");
                }
            }

            private String label = dest;
            @Override
            public String readLabel()
            {
                return label;
            }
        };
    }

    public static void main(String[] args)
    {
        Test3 test3 = new Test3();
        Destination d3 = test3.destination("Wu Han", 101.1F);
        System.out.println(d3.readLabel());
    }
}

??如上,该匿名内部类中对cost进行了初始化,由于匿名内部类是没有名称的,所以初始化方法自然也是没有名称的了。

??在使用匿名内部类后,我们再使用工厂方法,该方法就可以变得十分简洁易用。

??在最后说一下为什么我们需要内部类,最吸引我们的一个原因是:每个内部类都能独立地继承一个(接口的)实现,所以无论外围类是否已经继承了某个(接口的)实现,对于内部类都没有影响。在上面我们也看到了,使用匿名内部类,匿名内部类会直接实现接口,这在某些条件下会给我们带来便捷性。

Java内部类之匿名内部类

标签:strong   read   cost   static   初始化   over   class   引用   ide   

原文地址:http://www.cnblogs.com/yangmingyu/p/6921254.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!