Can't include the same interface with different parameters?

前端 未结 2 1238
野性不改
野性不改 2021-01-04 18:34

Consider the following example:

public class Sandbox {
    public interface Listener {
        public void onEvent(T event);
             


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 18:54

    No. You cant. It's because generics are supported only at compiler level. So you can't do thinks like

    public interface AnotherInterface {
        public void onEvent(List event);
        public void onEvent(List event);
    }
    

    or implements interface with several parameters.

    upd

    I think workaround will be like this:

    public class Sandbox {
    //    ....
        public final class JPanelEventHandler implements Listener {
            AnotherInterface target;
            JPanelEventHandler(AnotherInterface target){this.target = target;}
            public final void onEvent(JPanel event){
                 target.onEvent(event);
            }
        }
    ///same with JLabel
    }
    

提交回复
热议问题