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

时间:2016-02-09 20:51:23

标签: java methods polymorphism subclass superclass

我正在学习多态,我在我的超类中得到了这条红线,并且我对我的代码进行了评论:

    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:修正了类。

4 个答案:

答案 0 :(得分:1)

你的动物类应该是这样的

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返回类型。构造函数不应该有返回类型。其次,您的getName()方法的返回类型为void。为了使其正常工作,您需要声明它返回的内容。鉴于此,我会留给您实现其余的代码。

答案 1 :(得分:0)

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

public Dog(...)

public void Dog(...)

同样适用于Animals

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

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

答案 2 :(得分:0)

看起来getName()的返回类型设置为void,并且您尝试返回String。在此方法中将void更改为String,红线应该在那里消失。您在getColour()中也遇到了同样的问题。

除此之外,您的构造函数不能包含返回类型,因此您应该删除void

您的专家(Dog) puppy.getColour();正在尝试将从String返回的puppy.getColour()转换为Dog个对象。我相信您尝试做的是在getColour()上致电puppy,但您注意到它首先必须是Dog。请尝试使用此((Dog)puppy).getColour();,以便将puppy投射到Dog,然后您可以调用所需的方法。

答案 3 :(得分:0)

[增订]

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

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

    }
}