子类父类转换
package com.neuedu.day06;
import com.neuedu.day05.homework.A1;
import com.neuedu.day05.homework.B1;
import com.neuedu.day05.homework.C1;
import com.neuedu.day05.homework.Rectangle;
import com.neuedu.day05.homework.Shape;
public class FatherSonChange {
public static void main(String[] args) {
Rectangle r = new Rectangle("red", 30, 20);
r.showAll();
Shape s = r;
Rectangle r_new = (Rectangle) s;
System.out.println(r_new.getWidth());
A1 a = new B1();
if (a instanceof B1)
{
B1 b = (B1) a;
System.out.println(b.getHeight());
} else {
System.out.println("类型不匹配");
}
A1 a1 = new A1();
if (a1 instanceof B1)
{
B1 b = (B1) a1;
System.out.println(b.getHeight());
}
else {
System.out.println("类型不匹配");
}
}
}