Implementing Generic Interface in Java

后端 未结 7 2011
忘了有多久
忘了有多久 2020-12-24 12:38

I have a Java generics question I was hoping someone could answer. Consider the following code:

public interface Event{}
public class AddressChanged implemen         


        
7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 13:11

    You can't do that in Java. You can only implement one concrete realization of the same generic interface. I would do this instead:

    public class AddressHandler implements Handles{
        public void handle(Event e){
          if(e instanceof AddressDiscarded){
             handleDiscarded(e);
          } else if(e instanceof AddressChanged){
             handleChanged(e);
          }
        }
        public void handleDiscarded(AddressDiscarded e){}
        public void handleChanged(AddressChanged e){}
    }
    

提交回复
热议问题