22. JDK14的新特性:instanceof模式匹配
@Data
public class Girraffe {
private String name;
}@Data
public class Hippo {
private String name;
} public void testZooOld(Object animal){
if(animal instanceof Girraffe){
Girraffe girraffe = (Girraffe) animal;
log.info("girraffe name is {}",girraffe.getName());
}else if(animal instanceof Hippo){
Hippo hippo = (Hippo) animal;
log.info("hippo name is {}",hippo.getName());
}
throw new IllegalArgumentException("对不起,该动物不是地球上的生物!");
}最后更新于
这有帮助吗?