Implementing Generic Interface in Java

后端 未结 7 2036
忘了有多久
忘了有多久 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 12:50

    AFAIK you cannot do that, because when compiling the source code in Java these will both boil down to handle(Event), making the method ambiguous.

    The generic information is not available during runtime in Java, in contrast to C#. That is why there it works as you describe.

    You will have to change the method names to make them unique, like handleAddressChanged and handleAddressDiscarded.

    This is indeed one of the weak points of Java generics.

提交回复
热议问题