• Java内部类之匿名内部类


      我们都知道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进行了初始化,由于匿名内部类是没有名称的,所以初始化方法自然也是没有名称的了。

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

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

  • 相关阅读:
    在有多个input的表单当中按回车按钮,如何不自动提交表单,而触发某个按钮的事件
    login.jsp
    json数据源
    translation.js
    cookie.js
    i18next.min.js
    struts2拦截器实现原理
    proxy-target-class="true" 与proxy-target-class="false"的区别(声明事务的时候)
    NavLink
    【转载】target='_blank' 安全漏洞示例
  • 原文地址:https://www.cnblogs.com/liuyang0/p/6271317.html
Copyright © 2020-2023  润新知