从接口对象转换为实现它的 class 的对象?
Casting from an interface object to an object of a class that implements it?
我正在尝试转换接口对象:
ScreenController startScreenController = nifty.getScreen("start").getScreenController();
(ScreenController是一个接口)
到 class 名为 PrisonStartScreenControl 的对象:
startScreenController(PrisonStartScreenControl).setNifty(nifty);
^ 但此行会导致错误。我知道 startScreenController 对象等于 PrisonStartScreenControl,所以我如何才能将接口转换为其实现者 -class?
转换需要在要转换的变量的左侧,而不是右侧,例如:
((PrisonStartControl)startScreenController).setNifty(nifty);
但是,你为什么要投射? setNifty() 应该是接口上的一个方法,所以不需要转换。
我正在尝试转换接口对象:
ScreenController startScreenController = nifty.getScreen("start").getScreenController();
(ScreenController是一个接口)
到 class 名为 PrisonStartScreenControl 的对象:
startScreenController(PrisonStartScreenControl).setNifty(nifty);
^ 但此行会导致错误。我知道 startScreenController 对象等于 PrisonStartScreenControl,所以我如何才能将接口转换为其实现者 -class?
转换需要在要转换的变量的左侧,而不是右侧,例如:
((PrisonStartControl)startScreenController).setNifty(nifty);
但是,你为什么要投射? setNifty() 应该是接口上的一个方法,所以不需要转换。