开始使用免费开始使用

在 Tesla 类上实现接口

为了让 Tesla 类具备自动驾驶功能,您将使用 ElectricCar 接口来启用该功能。在 Tesla 类中,您需要利用该接口并按要求实现自动驾驶方法。

NOTE接口中非 void 的方法必须在实现该接口的类中实现

本练习是课程的一部分

Java 面向对象编程入门

查看课程

练习说明

  • Tesla 类上使用 implements 关键字实现 ElectricCar 接口。
  • Tesla 类中以 public 方法实现 void 类型的 activateSelfDriving,被调用时打印 "self driving on"
  • myTesla 对象实例上调用 activateSelfDriving 方法。

交互式实操练习

通过完成这段示例代码来试试这个练习。

public class Main {
    
    interface ElectricCar {
        void activateSelfDriving();
    }
           
    // Implement ElectricCar Interface
    static class Tesla ____ ____ {
        
        // Implement activateSelfDriving method 
        public void ____() {
            System.out.____("self driving on");
        }                              
    }

    public static void main(String[] args) {
        Tesla myTesla = new Tesla();
        // Call activateSelfDriving method
        ____.____();
    }
}
编辑并运行代码