A brief introduction to anonymous inner classes is given in the Java Improvement Chapter - Detailed Explanation of Internal Classes. However, there are still many other detailed problems with internal classes, so this blog was derived. In this blog, you can learn about the use of anonymous inner classes, things to pay attention to in anonymous inner classes, how to initialize anonymous inner classes, and why the formal parameters used by anonymous inner classes must be final.
new 父类构造器(参数列表)|实现接口() { //匿名内部类的类体部分 }
public abstract class Bird { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract int fly(); } public class Test { public void test(Bird bird){ System.out.println(bird.getName() + "能够飞 " + bird.fly() + "米"); } public static void main(String[] args) { Test test = new Test(); test.test(new Bird() { public int fly() { return 10000; } public String getName() { return "大雁"; } }); } } ------------------ Output: 大雁能够飞 10000米
public class WildGoose extends Bird{ public int fly() { return 10000; } public String getName() { return "大雁"; } } WildGoose wildGoose = new WildGoose(); test.test(wildGoose);
## 3. Why should the formal parameters used be final
Reference file: http://android.blog.51cto.com/268543/384844
When we pass parameters to anonymous inner classes, if the formal parameter needs to be used in the inner class, then the formal parameter must be final. That is to say: when the formal parameter of the method needs to be used in the inner class, the formal parameter must be final.
Why does it have to be final?
First of all, we know that after the internal class is successfully compiled, it will generate a class file. This class file is not the same class file as the external class. It only retains references to the external class. When the parameters passed in by the external class need to be called by the internal class, from the perspective of the Java program, they are called directly:
public class OuterClass { public void display(final String name,String age){ class InnerClass{ void display(){ System.out.println(name); } } } }
From the above code, it seems that the name parameter should be called directly by the internal class? In fact, it is not the case. The actual operation after java compilation is as follows:
public class OuterClass$InnerClass { public InnerClass(String name,String age){ this.InnerClass$name = name; this.InnerClass$age = age; } public void display(){ System.out.println(this.InnerClass$name + "----" + this.InnerClass$age ); } }
So from the above code, the inner class does not directly call the parameters passed by the method, but uses its own constructor to process the incoming parameters. Backup, what your internal method calls are actually your own properties rather than the parameters passed in by the external method.
直到这里还没有解释为什么是final?在内部类中的属性和外部方法的参数两者从外表上看是同一个东西,但实际上却不是,所以他们两者是可以任意变化的,也就是说在内部类中我对属性的改变并不会影响到外部的形参,而然这从程序员的角度来看这是不可行的,毕竟站在程序的角度来看这两个根本就是同一个,如果内部类该变了,而外部方法的形参却没有改变这是难以理解和不可接受的,所以为了保持参数的一致性,就规定使用final来避免形参的不改变。
简单理解就是,拷贝引用,为了避免引用值发生改变,例如被外部类的方法修改等,而导致内部类得到的值不一致,于是用final来让该引用不可改变。
故如果定义了一个匿名内部类,并且希望它使用一个其外部定义的参数,那么编译器会要求该参数引用是final的。
四、匿名内部类初始化
我们一般都是利用构造器来完成某个实例的初始化工作的,但是匿名内部类是没有构造器的!那怎么来初始化匿名内部类呢?使用构造代码块!利用构造代码块能够达到为匿名内部类创建一个构造器的效果。
public class OutClass { public InnerClass getInnerClass(final int age,final String name){ return new InnerClass() { int age_ ; String name_; //构造代码块完成初始化工作 { if(0 < age && age < 200){ age_ = age; name_ = name; } } public String getName() { return name_; } public int getAge() { return age_; } }; } public static void main(String[] args) { OutClass out = new OutClass(); InnerClass inner_1 = out.getInnerClass(201, "chenssy"); System.out.println(inner_1.getName()); InnerClass inner_2 = out.getInnerClass(23, "chenssy"); System.out.println(inner_2.getName()); } }
以上就是 java提高篇(九)-----详解匿名内部类的内容,更多相关内容请关注PHP中文网(www.php.cn)!