Here's an implementation of as, as suggested by @Omar Kooheji:
public static <T> T as(Class<T> clazz, Object o){
if(clazz.isInstance(o)){
return clazz.cast(o);
}
return null;
}
as(A.class, new Object()) --> null
as(B.class, new B()) --> B
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…