用法1)
--------------------------
import java.awt.*;
import java.awt.event.*;
class xxt
{
static Button b; public static void main(String in[])
{
Frame f=new Frame();
f.setVisible(true);
f.setSize(500,500);
f.setLayout(new FlowLayout());
b=new Button("close");
f.add(b); b.addActionListener(new l()); //不同点
}
}class l implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== xxt.b)
System.exit(0);
}
} ---------------------------
用法2)
---------------------------
import java.awt.*;
import java.awt.event.*;
class xt
{
static Button b; public static void main(String in[])
{
Frame f=new Frame();
f.setVisible(true);
f.setSize(500,500);
f.setLayout(new FlowLayout());
b=new Button("close");
f.add(b);
b.addActionListener //不同点
(
new l()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== xt.b)
System.exit(0);
}
}
);
}
}class l implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
---------------------------
两个程序的执行效果完全一样,
代码的不同点仅在addActionListener方法的参数,其他部分也都一样.
我对用法2不太理解:new l()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== xt.b)
System.exit(0);
}
}
这个是构造方法吗?
按理说,构造方法中的语句,在创建对象时就会被执行的.但是actionPerformed方法应该是在发生事件时由系统自动调用的,
把它放到构造方法里有什么用呢?
--------------------------
import java.awt.*;
import java.awt.event.*;
class xxt
{
static Button b; public static void main(String in[])
{
Frame f=new Frame();
f.setVisible(true);
f.setSize(500,500);
f.setLayout(new FlowLayout());
b=new Button("close");
f.add(b); b.addActionListener(new l()); //不同点
}
}class l implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== xxt.b)
System.exit(0);
}
} ---------------------------
用法2)
---------------------------
import java.awt.*;
import java.awt.event.*;
class xt
{
static Button b; public static void main(String in[])
{
Frame f=new Frame();
f.setVisible(true);
f.setSize(500,500);
f.setLayout(new FlowLayout());
b=new Button("close");
f.add(b);
b.addActionListener //不同点
(
new l()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== xt.b)
System.exit(0);
}
}
);
}
}class l implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
}
}
---------------------------
两个程序的执行效果完全一样,
代码的不同点仅在addActionListener方法的参数,其他部分也都一样.
我对用法2不太理解:new l()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== xt.b)
System.exit(0);
}
}
这个是构造方法吗?
按理说,构造方法中的语句,在创建对象时就会被执行的.但是actionPerformed方法应该是在发生事件时由系统自动调用的,
把它放到构造方法里有什么用呢?
构建了一个继承了SomeClass的匿名对象(没有出现类名的对象),相当于
class AnotherClass extends SomeClass{......(类中定义部分)}
你得到的对象是就是 new AnotherClass();
只不过你现在用new SomeClass(){......}直接得到了它,中间没用到一个新的类名,但你得到的是一个新的类的对象。
同理,
new SomeInterface(){......};
构建了一个实现了SomeInterface的匿名对象。
用法2)
---------------------------
import java.awt.*;
import java.awt.event.*; class xt
{
static Button b; public static void main(String in[])
{
Frame f=new Frame();
f.setVisible(true);
f.setSize(500,500);
f.setLayout(new FlowLayout()); b=new Button("close");
f.add(b);
b.addActionListener //不同点
(
new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource()== xt.b)
System.exit(0);
}
}
);
}
}