publicclasscomputer{
usb u;publicvoidinput(String msg){
System.out.println("计算机录入:"+msg);
u.sendMsg(msg);}}
telephone类(接收)
publicclasstelephone{int count=100;
usb u =newusb(){@OverridepublicvoidsendMsg(String msg){
telephone.this.count=200;
System.out.println("手机收到:"+msg);}};}
test类
publicclasstest{publicstaticvoidmain(String[] args){
computer c =newcomputer();//发送方:定义接口,调用接口方法
telephone p =newtelephone();//接收方:定义接口,实现接口
c.u = p.u;//监听器:将接受方实现的接口给发送方
c.input("啊,我死了");}}