簡體   English   中英

解析Java中的隱藏類型參數

[英]Resolving hidden Type Parameter in Java

考慮以下代碼:

class Scratch<T> {
  class InnerClass<T> {
    public void executeHiddenMethod(){
     //..some code to use Inner (T) type
     T r = null; //declared T from inner T type
     //..some code to use Outer (T) type
     //?? How to use outer T type?
    }
  }


//when trying to invoke the code:
public static void main(String[] args) {
    Scratch<String> scr = new Scratch<>();
    Scratch<String>.InnerClass<Double> d = scr.new InnerClass<>();
    d.executeHiddenMethod();
  }
}
  • 有沒有辦法將外部 Class Scratch的隱藏類型參數揭示到內部InnerClass
  • 或者 JLS 中是否有任何子句禁止在類型參數被隱藏的情況下公開這種情況?

除非我誤解了這個問題,否則您應該能夠為外部和內部 class 使用不同的類型參數T代表ScratchS代表InnerClass

 class Scratch<T> {
      class InnerClass<S> {
        public void executeHiddenMethod(){
         ...
         S s = null; 
         ...
         T t = null;
        }
      }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM