我正在尝试投射一个接口对象:

ScreenController startScreenController = nifty.getScreen("start").getScreenController();


(ScreenController是一个接口)

到名为PrisonStartScreenControl的类的对象:

startScreenController(PrisonStartScreenControl).setNifty(nifty);


^,但此行会导致错误。我知道startScreenController对象等于PrisonStartScreenControl,那么如何将接口强制转换为其实现者类?

最佳答案

转换必须在要转换的变量的左侧,而不是右侧,例如:

((PrisonStartControl)startScreenController).setNifty(nifty);


但是,您为什么仍然要投射? setNifty()应该是接口上的方法,因此无需强制转换。

07-24 09:51
查看更多