例子
//定义接口:其实就是定义类
//我接口中并没有将方法实现
interface myInterface{
int getNum();
String getName();
}
//定义类,声明了接口
public class stu implement myInterface{
.....
//添加你自己的代码.....//下面实现接口
int getNum(){
.......
return xxx;
}
String getName(){
......
return yyy;}}
//定义接口:其实就是定义类
//我接口中并没有将方法实现
interface myInterface{
int getNum();
String getName();
}
//定义类,声明了接口
public class stu implement myInterface{
.....
//添加你自己的代码.....//下面实现接口
int getNum(){
.......
return xxx;
}
String getName(){
......
return yyy;}}
//你可以看以下下面的代码,我能正常编译运行的import java.util.*;public class Item2 implements Comparable {
private String id;
private String name;
private double retail;
private int quantity;
private double price;
private boolean noDiscount; Item2(String idIn, String nameIn, String retailIn, String quanIn, String discountIn) {
id = idIn;
name = nameIn;
retail = Double.parseDouble(retailIn);
quantity = Integer.parseInt(quanIn);
if (discountIn.equals("TRUE"))
noDiscount = true;
else
noDiscount = false;
if (quantity > 400)
price = retail * .5D;
else if (quantity > 200)
price = retail * .6D;
else
price = retail * .7D;
price = Math.floor( price * 100 + .5 ) / 100;
if (noDiscount)
price = retail;
} public int compareTo(Object obj) {
Item2 temp = (Item2)obj;
if (this.price < temp.price)
return 1;
else if (this.price > temp.price)
return -1;
return 0;
} public String getId() {
return id;
} public String getName() {
return name;
} public double getRetail() {
return retail;
} public int getQuantity() {
return quantity;
} public double getPrice() {
return price;
}
}
//define a event object,the object will tell the consumer which window throws the event
//a event object is a object that will take the data from source to dest,
//or a protocal between two objects(here are Event source object and Event listener)
class WndEvent{
Wnd m_oWnd;
WndEvent(Wnd src){m_oWnd=src;}
String getWnd(){return m_oWnd.toString();};
}//define a listener that process the window event
interface WndListener{
void wndOpen(WndEvent e);
void wndClose(WndEvent e);
}//a listener implemention
class AWndListener implements WndListener{
public void wndOpen(WndEvent e){
System.out.println("window ["+ e.getWnd() + "] opened.");
} public void wndClose(WndEvent e){
System.out.println("window ["+ e.getWnd() + "] closed.");
}
}//a event source object
class Wnd{
String m_strWndName;
Wnd(String strWndName){m_strWndName=strWndName;} void initShow(){
//show the window first time
//after accomplish this,call wndOpen()
for(int i=0;i<vListeners.size();i++){
((WndListener)vListeners.get(i)).wndOpen(new WndEvent(this));
}
} void close(){
//close the window
//after accomplish this,call wndClose()
for(int i=0;i<vListeners.size();i++){
((WndListener)vListeners.get(i)).wndClose(new WndEvent(this));
}
} public String toString(){
return m_strWndName;
} Vector vListeners = new Vector();
void addListener(WndListener el){
vListeners.add(el);
}
}public class test{
public static void main(String[] args){
Wnd wnd = new Wnd("sample window a");//create source object //"通过调用源对象中特定的方法注册带有源的监听器对象"
wnd.addListener(new AWndListener());//add a listener
//add another listener(a anonymous class)
wnd.addListener(new WndListener(){
public void wndOpen(WndEvent e){
System.out.println("I'v got window ["+ e.getWnd() + "] opened event.");
} public void wndClose(WndEvent e){
System.out.println("I'v got window ["+ e.getWnd() + "] closed event.");
}
}
);
//running
wnd.initShow();//throws out event,src object will automatically call corresponding method of listeners registered ,here the method is wndOpen()
wnd.close();//throws out event,src object will automatically call corresponding method of listeners registered ,here the method is wndClose()
}
}上述是现想的,没测试过,也是我对此理解的一个整理,希望大家能明白,有问题欢迎交流本来在另外一个帖子里回了,但窗口已关闭,找不到URL了,好在EmEditor还没关,就又贴了过来
addActionListener(...);
addKeyListener(...);
addMouseListener(...);
...
方法注册监听器类