如何用接口实现,回调呢,C++可以传函数指针,java怎么办啊?,先谢了谢谢

解决方案 »

  1.   

    java有java.lang.reflect.Method类可以实现。
    接口除了可以向上转型,也是一种协议,实现了这个接口,就必须遵循接口里面的方法签名。
      

  2.   


    给你一个JAVA常用的回调方式,JAVA里面一般都是这样用的。//1.需要回调的类
    public class A{ 
     public int testCallback(ICallback  ac){
       ......
       Object result=ac.invoke(..);
       .....
     }
    }
    //2.回调接口
    public interface ICallback{
      Object invoke(...);
    }//3.实现回调接口
    public class Callback implemnts ICallback{
      .......
     }public class Test{
     public static void main(...){
      A a=new A();
      a.testCallback(new Callback ());
    }
    }
      

  3.   


    一般情况下,回调接口都是直接采用匿名类的方式来实现的,呵呵,在JAVA里实现回调就是这么麻烦,没有办法
      

  4.   

    可是我感觉,方法签名,无非是为了,向上转型,A 实现了 p, 一定有个类B的某个方法,使用了p, 是这样的吗
      

  5.   

    匿名类在哪呢啊 ?===========在这里,很多时候是这样写的:
    public class Test{
     public static void main(...){
      A a=new A();
      a.testCallback(new  ICallback(){
         ……
         Object invoke(...);
         ……
      });
    }
    } PS.为什么我的FireFox无法使用“插入源代码”功能????!!!!
      

  6.   

    Java 的接口支持提供了一种获得回调的等价功能的机制。其技巧就是:定义一个简单接口,并在该接口中声明我们要调用的方法。例如,假定我们希望在某个事件发生时得到通知。我们可以定义一个接口:public interface InterestingEvent
    {
    // 这仅是一个常规方法。因此如果需要,
    // 它可有返回值,也可接收参数。
    public void interestingEvent ();
    }这使得我们可以控制实现该接口的类的任何对象。因此,我们不必关心任何外部类型信息。与在将 C++ 代码用于 Motif 时使用窗口小部件的数据域来容纳对象指针的难以控制的 C 函数相比,这种方法要好得多。发出事件信号的类必须等待实现了 InterestingEvent 接口的对象,并在适当时候调用 interestingEvent() 方法。public class EventNotifier
    {
    private InterestingEvent ie;
    private boolean somethingHappened;public EventNotifier (InterestingEvent event)
    {
    // 保存事件对象以备后用。
    ie = event;// 还没有要报告的事件。
    somethingHappened = false;
    }//...public void doWork ()
    {
    // 检查在别处设置的谓词。
    if (somethingHappened)
    {
    // 通过调用接口的这个方法发出事件信号。
    ie.interestingEvent ();
    }
    //...
    }// ...
    }在上例中,我使用 somethingHappened 谓词来跟踪是否应触发事件。在许多情况下,调用此方法足以保证向 interestingEvent() 发出信号。希望接收事件通知的代码必须实现 InterestingEvent 接口,并将自身引用传递给事件通知程序。public class CallMe implements InterestingEvent
    {
    private EventNotifier en;public CallMe ()
    {
    // 创建事件通知程序,并将自身引用传递给它。
    en = new EventNotifier (this);
    }// 为事件定义实际的处理程序。
    public void interestingEvent ()
    {
    // 噢!必定发生了感兴趣的事件!
    // 执行某些操作 ...
    }//...
    }
      

  7.   

    楼上的有点错:
    public class Test{
    public static void main(...){
    A a=new A();
    a.testCallback(new  ICallback(){
    ……
    Object invoke(...){....};
    ……
    });
    }
    }
      

  8.   

    向上转型这个技术就很有用了啊1.实现接口的类就相当于让这个类看起来像什么,比如说实现runnable接口的类,看起来就像一个线程,所以每个实现runnable接口的类可以当作线程来用。2.接口还有一个很重要的用途就是maker interface,也是让接口看起来像什么
    如cloneable接口,任何实现cloneable接口的类都可以被克隆
    上面两点结合向上转型技术,就说明通过接口实现多态性
    另外:接口事实上充当一个和其他类交互的角色,其他人要跟你所设计的系统进行交互,可能只需要知道你的接口是怎样就行了,背后具体怎么设计不需要关心。从这方面讲,接口实现了封装性。当然,这也要求你所设计的接口要稳定,不能随便改变。如果一改变的话,其他跟你交互的系统也要修改。接口也相当于一个描述性的东西,告诉你,实现了这个接口的类到底能做些什么。接口有点类似于sun公司提出了jvm的specification,告诉大家虚拟机应该有些什么功能,但是具体怎么实现这个虚拟机就由各大公司实现,如IBM的jvm,MS的JVM,SUN的JVM,这些jvm看起来、用起来都差不多,因为他们只不过是实现了sun公司的jvm的specification.这个specification就有点像接口。扯了这么多,不知道lz看懂了没?
      

  9.   

    11楼的程序,好像没写完阿,没有内部类阿!!!,EventNotifier先执行吗?
      

  10.   

    类是对实际对象的抽象,而且为了避免由于类继承而带来的“菱形”问题,所以Java只支持单继承,但是接口支持多继承,为什么接口不会产生“菱形”问题呢,因为接口中并没有实现方法,而是由类实现,所以即使有“菱形”问题的存在,JRE也不会产生疑惑。而且,由于接口不包含实现,所以可以把接口看成比类更加抽象的类型.
      

  11.   

    jhkhjkhjk<tr align="left">
    <td colspan="5">
    <table width="97%" border="0" align="center">
    <tr>
    <td>
    欢迎你访问论坛:<%=username%>
    </td>
    <td align="right">
    <A href="<%=request.getContextPath()%>/backtoindex.do"><tr align="left">
    <td colspan="5">
    <table width="97%" border="0" align="center">
    <tr>
    <td>
    欢迎你访问论坛:<%=username%>
    </td>
    <td align="right">
    <A href="<%=request.getContextPath()%>/backtoindex.do">
    首页
    </A>&nbsp;
    <A href="logoff.do">
    <tr align="left">
    <td colspan="5">
    <table width="97%" border="0" align="center">
    <tr>
    <td>
    欢迎你访问论坛:<%=username%>
    </td>
    <td align="right">
    <A href="<%=request.getContextPath()%>/backtoindex.do">
    首页
    <tr align="left">
    <td colspan="5">
    <table width="97%" border="0" align="center">
    <tr>
    <td>
    欢迎你访问论坛:<%=username%>
    </td>
    <td align="right">
    <A href="<%=request.getContextPath()%>/backtoindex.do">
    首页
    </A>&nbsp;
    <A href="logoff.do">
    注销登录
    </A>&nbsp;
    <A href="<%=request.getContextPath()%>/newtopic.do">
    发新话题
    </A>
    </td>
    </tr>
    </table>
    </td>
    </tr> </A>&nbsp;
    <A href="logoff.do">
    注销登录
    </A>&nbsp;
    <A href="<%=request.getContextPath()%>/newtopic.do">
    发新话题
    </A>
    </td>
    </tr>
    </table>
    </td>
    </tr> 注销登录
    </A>&nbsp;
    <A href="<%=request.getContextPath()%>/newtopic.do">
    发新话题
    </A>
    </td>
    </tr>
    </table>
    </td>
    </tr>
    首页
    </A>&nbsp;
    <A href="logoff.do">
    注销登录
    </A>&nbsp;
    <A href="<%=request.getContextPath()%>/newtopic.do">
    发新话题
    </A>
    </td>
    </tr>
    </table>
    </td>
    </tr>kjkjkjk
      

  12.   

    反对2楼,java只允许单继承,如果实现所谓的多继承也不是用接口,而是内部类
      

  13.   

    我想二楼的意思是java可以使用接口达到多继承同样的功能,至于java不允许多继承,我想没人不知道嘀!内部类适用范围没有接口那么广,如果确定指在一个类中用到它的话还好,否则还是用接口比较好
      

  14.   

    一般用匿名内部类实现callback 
    public interface YourInterface {    public void interfaceMethod(); } 
    public class YourClass{    public void classMethod(YourInterface yInterface) 
       { 
             ...... 
             yInterface.interfaceMethod(); 
             ...... 
        } 
        
      public static void main(String[] args) 
      { 
          YourClass yClass = new YourClass();       yClass.classMethod(new YourInterface() 
           { 
               public void interfaceMethod() 
               { 
                   //do sth 
                   System.out.print("hello world."); 
                   .......      
               } 
           }); 
    }