原始类型的Java类型擦除'会员

时间:2016-03-12 12:39:36

标签: java generics type-erasure typechecking

我很好奇Java的类型擦除的具体规则会导致以下情况发生:

import java.util.function.Supplier;

public class TypeCheckerWeirdness {

    public static class Meow<S extends Meow> {

        public Supplier<Boolean> hungry;

    }

    public static void main(String[] args) {
        Meow<?> instanceA = new Meow<>();
        Meow instanceB = new Meow<>();

        // fine
        boolean x1 = instanceA.hungry.get();

        // incompatible types. Found: 'java.lang.Object', required: 'boolean'
        boolean x2 = instanceB.hungry.get();
    }

}

是否因为如果您使用原始类型,即使在编译时,该类型成员的所有泛型信息都将被删除?

0 个答案:

没有答案