Java从接口中的超类调用方法
我目前正在开发一个需要我控制机器人的项目 我把它们放在一系列机器人界面中,事实是,我得到了一个名为RobotMovement的超级类,因为所有机器人的所有运动都是平等的 实现RobotInterface的robot类也扩展了Super类 如何从接口数组中的超类调用move()方法 实现RobotInterface的robot类也扩展了Super类 如何从接口数组中的超类调用move()方法 您可以在Java从接口中的超类调用方法,java,Java,我目前正在开发一个需要我控制机器人的项目 我把它们放在一系列机器人界面中,事实是,我得到了一个名为RobotMovement的超级类,因为所有机器人的所有运动都是平等的 实现RobotInterface的robot类也扩展了Super类 如何从接口数组中的超类调用move()方法 实现RobotInterface的robot类也扩展了Super类 如何从接口数组中的超类调用move()方法 您可以在RobotInterface中声明move()方法。这样,Java将允许您对任何类型的RobotI
RobotInterface
中声明move()
方法。这样,Java将允许您对任何类型的RobotInterface
表达式调用move()
,并且Java将强制要求RobotInterface
的所有实例都实现move()
类RobotMovement{
公开作废动议(){
System.out.println(“移动…”);
}
}
接口机器人接口{
public void move();//添加此
}
类Robot扩展RobotMovement实现RobotInterface{
}
班长{
公共静态void main(字符串[]args){
列表=新的ArrayList();
添加(新机器人());
添加(新机器人());
用于(机器人接口ri:列表){
ri.move();
}
}
}
代码示例将有助于。。。
class RobotMovement {
public void move() {
System.out.println("moving...");
}
}
interface RobotInterface {
public void move(); // add this
}
class Robot extends RobotMovement implements RobotInterface {
}
class Main {
public static void main(String[] args) {
List<RobotInterface> list = new ArrayList<RobotInterface>();
list.add(new Robot());
list.add(new Robot());
for (RobotInterface ri: list) {
ri.move();
}
}
}