1

次の Java コードがあるとします。

public class Test {
    public static class A<T> {
        private T t;

        public A(T t) {
            this.t = t;
        }

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }
    }

    public static class B<T> {
        private T t;

        public B(T t) {
            this.t = t;
        }

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }
    }

    public static class F<T> {
        private T t;

        public F(T t) {
            this.t = t;
        }

        public A<B<T>> construct() {
            return new A<>(new B<>(t));
        }

        public T getT() {
            return t;
        }

        public void setT(T t) {
            this.t = t;
        }
    }

    public static void main(String[] args) {
        F<?> f = new F<>(0);
        // 1: KO
        // A<B<?>> a = f.construct();
        // 2: KO
        // A<B<Object>> a = f.construct();
        // 3: OK
        // A<?> a = f.construct();
    }
}

クラスのメイン メソッドで、Test次の結果を受け取る変数の正しい型はf.construct()どれですか? このタイプは、私が探しているものはA<B<...>>どこにあるかのようなものでなければなりません。...

上記の 3 行のコードは、この問題を解決するための私の試みを表しています。1 行目と 2 行目が無効です。3 つ目ですが、B型情報が失われ、キャストする必要がありa.getT()ます。

4

1 に答える 1

1

A<? extends B<?>> a = f.construct();Paul Boddington が述べているように、正しい構文です。

于 2016-03-30T12:36:42.233 に答える