1.
public class text {public static void main(String[] args) {animal d=new dog();d.eat();// dog a = (dog) d;//类似强制转换//a.lookhome();/* if(d instanceof dog){dog a=(dog)d;a.lookhome();}else if(d instanceof cat){cat c=(cat) d;c.work();}else{System.out.println("没有这个类型,无法转换");}*/if(d instanceof dog a){a.lookhome();}else if(d instanceof cat c){c.work();}else{System.out.println("没有这个类型,无法转换");}} }class animal{public void eat(){System.out.println("在吃东西");}} class dog extends animal{@Overridepublic void eat() {System.out.println("狗在吃骨头");}public void lookhome(){System.out.println("狗在看家");} } class cat extends animal{@Overridepublic void eat() {System.out.println("猫在吃鱼");}public void work(){System.out.println("猫在抓老鼠");} }