public class Initial2 {
@SuppressWarnings("unused")
public static void main(String[] args) {
// TODO Auto-generated method stub
Dog dog=new Dog();
Animal animal=dog;//向上类型转换,隐式转换
if(animal instanceof Dog){
Dog dog2=(Dog)animal;//向下类型转换,强制类型转换
System.out.println("Dog型转换成了Animal型了");
}else{
System.out.println("Dog型不能转换成Animal型");
}
if(animal instanceof Cat){
Cat cat=(Cat)animal;
}else{
System.out.println("不能转换成Cat型");
}
System.out.println(dog);
System.out.println(animal);
System.out.println(dog2);//难道强制转换的对象不能打印
}
}