标签:类型 编译器 static 通配符 stat private his strong 不能
// A是B的父类
List list = new ArrayList<>();
list.add(new A());
list.add(new A());
list.add(new String());
System.out.println((A)list.get(0));
System.out.println((A)list.get(1));
// 编译时不报错,运行时报错:java.lang.ClassCastException
System.out.println((A)list.get(2));
// A是B的父类
List<A> list = new ArrayList<>();
list.add(new A());
list.add(new B());
// list.add(new String()); // 加上时,编译会报错
System.out.println((B)list.get(0));
// 编译时不报错,运行时报错:java.lang.ClassCastException
System.out.println((B)list.get(1));
A a1 = list.get(1);
if(a1.getClass() == B.class){
System.out.println((B)a1);
}
// A是B的父类
List<A> list = new ArrayList<>();
list.add(new A());
list.add(new B());
// 如果把B放入A的容器中,就把B当成A使用,不要想着强转回来使用了。
System.out.println(list.get(0));
System.out.println(list.get(1));
public class Main {
static class A {
}
static class B extends A {
}
static class C<T>{
private T t;
public C(){
}
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
}
}
public class Main {
public static void main(String[] args) {
C<A> cA = new C<>();
// 类型强转:这就是为什么1.1节中编译不报错,运行时报错的原因。编译时类型擦错,并不进行检查。
// Object obj = cA.getT();
// A t = (A)obj;
A t = cA.getT();
}
}
public static void main(java.lang.String[]);
Code:
0: new #2 // class Main$C
3: dup
4: invokespecial #3 // Method Main$C."<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #4 // Method Main$C.getT:()Ljava/lang/Object;
12: checkcast #5 // class Main$A
15: astore_2
16: return
public static void main(String[] args) {
C<A> cA = new C<>();
C<B> cB = new C<>();
// 为什么Class<? extends C> 而不是Class<C> Class<?> Class<? super C> ?
Class<? extends C> aClass = cA.getClass();
Class<? extends C> bClass = cB.getClass();
boolean b = aClass == bClass; // true
}
public static void main(String[] args) {
C<A> cA = new C<>();
C<B> cB = new C<>();
test(cA);
test(cB); // 编译时错误:Error: java: 不兼容的类型
}
public static void test(C<A> c){
A a = c.getT();
}
C<Object>
不是C<A>
的父类public static void main(String[] args) {
C<A> cA = new C<>(new A());
C<B> cB = new C<>(new B());
test(cA); // 编译时错误:Error: java: 不兼容的类型
test(cB); // 编译时错误:Error: java: 不兼容的类型
}
public static void test(C<Object> c) {
Object t = c.getT();
}
C<?>
接收所有类型public static void main(String[] args) {
C<A> cA = new C<>(new A());
C<B> cB = new C<>(new B());
test(cA);
test(cB);
}
public static void test(C<?> c) { // 或者C c也行
Object t = c.getT();
}
public static void main(String[] args) {
C<A> cA = new C<>(new A());
C<B> cB = new C<>(new B());
test(cA);
test(cB);
}
public static void test(C<? extends A> c) {
A t = c.getT();
}
public static void main(String[] args) {
C<A> cA = new C<>(new A());
C<B> cB = new C<>(new B());
test(cA);
test(new C<Object>());
test(cB); // 编译时报错:
}
public static void test(C<? super A> c) {
Object t = c.getT();
}
public static void main(String[] args) {
C<A> test = test();
test.setT(new A());
test.setT(new B());
test.setT(new Object()); // 编译错误
}
public static C<A> test() {
C<A> cA = new C<>(new A());
return cA;
}
public static void main(String[] args) {
C<?> test = test();
test.setT(new A()); // 编译错误
test.setT(new B()); // 编译错误
test.setT(new Object()); // 编译错误
}
public static C<?> test() {
C<A> cA = new C<>(new A());
return cA;
}
public static void main(String[] args) {
C<? extends A> test = test();
test.setT(new A()); // 编译错误
test.setT(new B()); // 编译错误
test.setT(new Object()); // 编译错误
}
public static C<? extends A> test() {
C<A> cA = new C<>(new A());
return cA;
}
public static void main(String[] args) {
C<? super A> test = test();
test.setT(new A());
test.setT(new B());
test.setT(new Object()); // 编译错误
}
public static C<? super A> test() {
C<A> cA = new C<>(new A());
return cA;
}
static class A {}
static interface D {}
// extends 类或接口 & 接口 & 接口 ...
static class C<T extends A & D & Comparable> {
private T t;
}
标签:类型 编译器 static 通配符 stat private his strong 不能
原文地址:https://www.cnblogs.com/linzhanfly/p/9761803.html