Retrieving type parameters from an instance of a generic base interface

前端 未结 9 888
悲&欢浪女
悲&欢浪女 2020-12-14 18:27

Given 2 interfaces:

public interface BaseInterface { }
public interface ExtendedInterface extends BaseInterface {}
         


        
9条回答
  •  失恋的感觉
    2020-12-14 18:45

    I don't think you can as these are really instance specific not class specific. Consider the following:

    List  a = new ArrayList();
    

    The fact that a is generic list of Strings is specific to the instance a and not to the class List. Thus, none of the methods of the List.class object could tell you that the genericized type would be of type String for a. Although MyClass in your example happens to have set values for the genricized types of the interface, i do not think this would be available at the interface Class object instance.

提交回复
热议问题