学习多态时对错误感到困惑

Confused about error while learning Polymorphism

我正在学习多态性,我在我的超类和子类中得到了这条红线,它在我的代码中有注释:

    public class Animals {
  private String name;

  public Animals(String name) {
      this.name = name;
        }

  public void changeName(String name){
      this.name= name;
  }

  public String getName(){
      return this.name;  // 
  }

}

这是我的子类:

public class Dog extends Animals {
    private String colour;

    public Dog(String name, String colour){
        super(name);  
        this.colour = colour;
    }

    public void changeColour(String colour) {
        this.colour = colour;
    }
    public String getColour(){
        return this.colour;
    }

}    

这是带有 main 方法的另一个脚本:

public class AnimalPolyTesting {
    public static void main(String[] args) {
        Animals puppy = new Dog("homie", "black"); // constructor Dog cannot be applied to given types;
        puppy.getName();
        (Dog) puppy.getColour(); // not a statement

    }
}

我不确定为什么会出现这些红线 编辑:代码运行但没有任何结果。 Edit2:修复了 类.

构造函数不能有 return 类型,因为它是隐式的(Dog 的构造函数显然 returns a Dog):

public Dog(...)

没有

public void Dog(...)

当然Animals也是如此。

而您的 get 方法声明了一个 void return 类型,这与意图相矛盾,因此更改为:

public String getName()...
public String getColour()...

看起来 getName() 设置为 return 类型的 void,而您正在尝试 return 类型 String。在此方法中将 void 更改为 String ,红线应该会在那里消失。您在 getColour().

中也有同样的问题

除此之外,您的构造函数中不能有 return 类型,因此您应该删除那些 void

您的行 (Dog) puppy.getColour(); 正试图将 return 从 puppy.getColour() 编辑的 String 转换为 Dog 对象。我相信您想要做的是在 puppy 上调用 getColour(),但您注意到它首先需要是 Dog。试试这个: ((Dog)puppy).getColour(); 这样你就可以将 puppy 转换为 Dog 然后你可以调用你想要的方法。

你的动物class应该是这样的

public class Animals {
    private String name;

    public Animals(String name) {
        this.name = name;
    }

    public void changeName(String name){
        this.name= name;
    }

    public String getName(){
        return this.name;
    }

}

您遇到的问题是您的构造函数具有 void return 类型。构造函数不应具有 return 类型。其次,您的 getName() 方法有一个 return 类型的 void。要使其正常工作,您需要声明它的内容 returning。鉴于此,我会留给您来实现您的其余代码。

[更新]

我已经在您的代码中注释了有问题的行。

public class Animals {
  private String name;

  /*
   * This is a method, not a constructor, because it has a return type.
   * Since there is no constructor, Java will automatically generate an implicit
   * `Animal` constructor for you -- one with no parameters.
   *
   * To fix: remove the return type.
   */
  public void Animals(String name) {
      this.name = name;
  }

  public void changeName(String name){
      this.name= name;
  }

  /*
   * The return type should be String, not void.
   */
  public void getName(){
      return this.name;  // RED LINE: Unexpected return value
  }
}

public class Dog extends Animals {
    private String colour;

    /*
     * 1. A "super(...)" call only makes sense from within a constructor, but this is not
     * a constructor, due to the void return type.
     * 2. Java will automatically generate an implicit `Dog` constructor for you -- one with no parameters.
     * 3. Since the only Animals constructor is the implicit constructor, which has no parameters,
     * the "super(...)" call would fail even if this was a constructor.
     * 
     * To fix: Fix Animals, and remove the void return type.
     */
    public void Dog(String name, String colour){
        super(name);  // RED LINE: constructor Animals in Animals class cannot be applied to given types; 
        this.colour = colour;
    }

    public void changeColour(String colour) {
        this.colour = colour;
    }

    /*
     * The return type should be String, not void.
     * 
     * To fix: change "void" to "String".
     */
    public void getColour(){
        return this.colour; //RED LINE: unexpected return value
    }
}

public class AnimalPolyTesting {
    public static void main(String[] args) {

        /*
         * The only Dog constructor is the implicit constructor, which has no parameters.
         * 
         * This will be fixed once Dog and Animals are fixed.
         */
        Animals puppy = new Dog("homie", "black"); // constructor Dog cannot be applied to given types;

        puppy.getName();

        /*
         * Dog.getColour() returns nothing (since its return type is void).
         * It makes no sense to try to coerce nothing to be of type Dog.
         * 
         * To call getColor(), you need to coerce puppy to be a Dog this way:
         * ((Dog) puppy).getColour();
         */
        (Dog) puppy.getColour(); // not a statement

    }
}