一个关于匿名内部类的构造器的问题

在网摘时看到一段这样的讨论,觉得有意思,主要内容如下:


我不知道我理解的匿名内部类和你理解的匿名内部类是否一样,不过应该是我这种理解,我给你举个例子:

import  java.awt. * ;
import  java.awt.event. * ;

public   class  a
{
public a()
{
System.out.println(
"eeeadfa");
}


public static void main(String[] agrs)
{
Frame f 
= new Frame();
Button b 
= new Button("OK");
b.addActionListener(
new ActionListener()
{
public ActionListener()
{
System.out.println(
"eeeeee");
}

public void actionPerformed(ActionEvent e)
{
System.out.println(
"aaaa");
}

}
);
f.add(b);
f.setSize(
100,100);
f.setVisible(
true);
new a();
}

}

 这里面的ActionListener应该就叫匿名内部类了。可是这段程序连编译都通过不了,除非注释掉ActionListener的constructor才能正常运行。所以不要说只有一个,一个constructor都不能有,因为它是匿名内部类。我想楼主说的应该不是匿名内部类,要么是内部类吧??我想那就应该可以有多个constructor了。不知道我这样的理解是否正确,仅供参考。

你这个语法不对,当然编译不过了!
匿名内部类的constructor直接用花括号括起来就行了,不能加名字的。上面的程序中你应该

//public ActionListener()
{
System.out.println("eeeeee");
}

这样程序就可以过了!其中花括号中的就是constructor了!

呵呵,这个问题可以结贴了。:)我自己想出来了,真是愚昧啊!只要试下用两次花括号括起来就有了两个constructor,编译可以运行的!!当然这两个constructor都不带参数,其实应该还是理解为实质上一个constructor。举例如下:

public   class  Parcel9  {
public Destination 
dest(
final String dest, final float price) {
return new Destination() {
private int cost;
// Instance initialization for each object:
{
cost 
= Math.round(price);
if(cost > 100)
System.out.println(
"Over budget!");
}

{
System.out.println(
"The second constructor!");
}


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

public static void main(String[] args) {
Parcel9 p 
= new Parcel9();
Destination d 
= p.dest("Tanzania"101.395F);
//d.cost100();
System.out.println(d.readLabel());
}

}
 



Output是:

Over budget!
The second constructor!
Tanzania


Anonymous classes cannot have constructors, Java invokes their superclass constructor implicitly.

是不是很矛盾,为了弄清这个问题,或从中得到起发,我们先看看这段代码.

 


/**
 * 
@author Aaron
 * 
 
*/

public   class  A  {
    
{
        System.out.println(
"1.I'm not sure who I am, so, I make the class A to test.");
    }
;
    
static {
        System.out.println(
"0.I'm a static block of A clasz.");
    }
;

    
public A(String string) {
        
this();
        System.out.println(string);
    }

    
    
public A(){
        System.out.println(
"2.no arguments constructor");
    }


    
public static void main(String[] agrs) {
        A a 
= new A("The first constructor");
        a 
= new A("The second constructor");
    }

}

程序运行结果如下:

0.I'm a static block of A clasz.
1.I'm not sure who I am, so, I make the class A to test.
2.no arguments constructor
The first constructor
1.I'm not sure who I am, so, I make the class A to test.
2.no arguments constructor
The second constructor

虽然这是一个我们熟得不能再熟的普通类了,但我想从其中能说明点问题.

1.先执行类中的静态块,这里要附带的讲一点就是,静态块实际是程序级的而不是类级的,什么意思呢,就是说不管你用不用类A,它都会在程序中被调用;并且是在main函数之前调用.
2."1.I'm not sure who I am, so, I make the class A to test."<==这是一个块; 并且有,每个类都可以申明多个"块",并且他在第次 new 的时候,在构造器之前被调用.
3.由第二点知识可以得知,Anonymous classes cannot have constructors是对的.而那个所谓"两个无参构造器"的段程序只是两个个块而已.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值