从接口对象转换为实现它的类的对象?

时间:2015-02-25 16:57:50

标签: java inheritance interface polymorphism

我正在尝试强制转换接口对象:

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

(ScreenController是一个界面)

到名为PrisonStartScreenControl的类的对象:

startScreenController(PrisonStartScreenControl).setNifty(nifty);

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

1 个答案:

答案 0 :(得分:2)

演员表必须位于要演员的左侧,而不是右侧,例如:

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

但是,你为什么还要施展? setNifty()应该是接口上的一个方法,所以不需要强制转换。