假设我有一个基类:
abstract class TheBase {}
class Foo extends TheBase {}
class Bar extends TheBase {}
我想将基础对象“转换”成这种类型:
TheBase obj = getFromSomewhere();
Foo foo = obj.asA(Foo.class);
Bar bar = obj.asA(Bar.class);
asA
将抛出我定义的异常,如:CustomCannotCastException()
这可能吗?