Java 从超类调用方法

Java 从超类调用方法,java,inheritance,Java,Inheritance,嗨。我试图通过一个子类从一个超类调用一个方法,但一直得到一个错误。 我试图调用的方法是setDestination(),但是编译器不断给我错误“找不到符号-方法setDestination(java.lang.string)”,我的讲师说这是方法调用和方法参数的简单不匹配,但是我有两个方法参数,类型都是String,所以我有点困惑 我的代码是: 超类车辆: public class Vehicle { // A unique ID for this vehicle

嗨。我试图通过一个子类从一个超类调用一个方法,但一直得到一个错误。 我试图调用的方法是
setDestination()
,但是编译器不断给我错误“找不到符号-方法setDestination(java.lang.string)”,我的讲师说这是方法调用和方法参数的简单不匹配,但是我有两个方法参数,类型都是
String
,所以我有点困惑

我的代码是:

超类车辆:

     public class Vehicle
     {
     // A unique ID for this vehicle
     private String id; 
     // The next destination of this Vehicle.
     private String destination;

     /**
     * Constructor for objects of class Vehicle
     */
     public Vehicle(String id)
     {
         destination = null;
     }

     /**
     * Return the ID of the Vehicle.
     * @return The ID of the Vehicle.
     */
     public String getID()
     {
        return id;
     }

     /**
     * Return the destination of the Vehicle.
     * @return The destination of the Vehicle.
     */
     public String getDestination()
     {
         return destination;
     }

     /**
     * Set the intented destination of the Vehicle.
     * @param destination The intended destination.
     */
     private void setDestination(String destination)
     {
         this.destination = destination;
     }
}
子类
滑行

public class Taxi extends Vehicle
{
    // The location of this taxi.
    private String location;
    // Whether it is free or not.
    private boolean free;

   /** 
    * Constructor for objects of class Taxi.
    * @param base The name of the company's base.
    * @param id This taxi's unique id.
    */
    public Taxi(String base, String id)
    {
         super(id);
         location = base;
         free = true;
    }

   /**
    * Book this taxi to the given destination.
    * The status of the taxi will no longer be free.
    * @param destination The taxi's destination.
    */
    public void book(String destination)
    {
        setDestination(destination);
        free = false;
    }

   /**
    * Return the status of this taxi.
    * @return The status.
    */
    public String getStatus()
    {   
        return getID() + " at " + location + " headed for " +
        destination;
    }

    /**
     * Return the location of the taxi.
     * @return The location of the taxi.
     */
     public String getLocation()
     {
         return location;
     }

     /**
     * Indicate that this taxi has arrived at its destination.
     * As a result, it will be free.
     */
     public void arrived()
     {
         location = destination;
         destination = null;
         free = true;
     }
 }

非常感谢您的帮助。

您已将您的
setDestination
定义为
private

private void setDestination(String destination) 

因此,您无法通过继承访问它。将其更改为受保护的或“公共”。了解有关访问修饰符的更多信息

这是一种
私有
方法。您不能访问私有方法。将其更改为受保护

您只能访问类中的私有成员

   Modifier Class   Package Subclass    World
    ---------------------------------------------
    public      Y      Y        Y           Y
    protected   Y      Y        Y           N
    no modifier Y      Y        N           N
    private     Y      N        N           N

私有方法不是继承的,超类引用调用自己的私有方法

使用
private void setDestination(字符串目的地)
代替

 public void setDestination(String destination) 

私有修饰符无法从类外访问