摘自Thinking in Java 第四版
思考:Anonymous Inner Class(匿名内部类)是否可以继承其它类?是否可以实现接口?
答案:匿名内部类与正规的继承相比有些受限,因为匿名内部类既可以继承类,也可以实现接口,但是不能两者兼备,实现接口也只能实现一个。
举例说明什么是匿名内部类:
class OuterMyTest {
public Test getFun(){
return new Test(){
private int i = 1;
public int value() {
return i;
}
};
}
public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test = aTest.getFun();
}
}
class Test {
void getFun(){
System.out.println("Test.getFun()");
}
}
getFun方法将返回值的生成与表示这个返回值的类的定义结合在一起,
这个类是匿名的,没有名字的。
创建一个继承自Test的匿名类的对象,通过new表达式返回的引用被自动向上转型为对Test的引用。
如果带参数 怎么办?
class OuterMyTest {
public Test getFun(int j){
return new Test(j){
private int i = 1;
public int value() {
return i;
}
};
}
public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test = aTest.getFun(0);
}
}
class Test {
public Test(int j) {
System.out.println("Test.Test");
}
void getFun(){
System.out.println("Test.getFun()");
}
}
如果是:
class OuterMyTest {
public Test getFun(int j){
return new Test(){
private int i = j;
public int value() {
return i;
}
};
}
public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test = aTest.getFun(0);
}
}
class Test {
public Test(int j) {
System.out.println("Test.Test(int j)");
}
public Test() {
System.out.println("Test.Test()");
}
void getFun(){
System.out.println("Test.getFun()");
}
}
编译报错:Cannot refer to the non-final local variable j defined in an enclosing scope
如果定义一个匿名内部类,并且希望它使用一个在其外部定义的对象,那么编译器要求其参数引用是final的。
如果被传递给匿名类的基类的构造器,它并不会在匿名类内部被直接使用,则不要求j是final的,
如果在匿名类内部使用,就要求j是final的
class OuterMyTest {
public Test getFun( final String dest,final float price){
return new Test(){
private int cost;
{
cost = Math.round(price);
if(cost>100){
System.out.println(cost);
}
}
private String lable = dest;
public String readLable() {
return lable;
}
};
}
public static void main(String[] args) {
OuterMyTest aTest = new OuterMyTest();
Test test = aTest.getFun("china", 109.12F);
}
}
class Test {
public Test(int j) {
System.out.println("Test.Test(int j)");
}
public Test() {
System.out.println("Test.Test()");
}
}