Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 重写父类_Java - Fatal编程技术网

Java 重写父类

Java 重写父类,java,Java,我有我的宠物超类,它有一个Dog子类,在我的超类中有一个特殊的方法是getSpecies()。在我的子类中,我希望能够返回super.getSpecies(),但也能返回该方法内部的另一个变量(在本例中是嗅觉) 超级类: public class Pet { protected int lifeSpan; protected String species, name, interaction; public Pet(){ } public Pet(i

我有我的宠物超类,它有一个Dog子类,在我的超类中有一个特殊的方法是getSpecies()。在我的子类中,我希望能够返回
super.getSpecies()
,但也能返回该方法内部的另一个变量(在本例中是嗅觉)

超级类:

public class Pet {
    protected int lifeSpan;
    protected  String species, name, interaction;

    public Pet(){
    }

    public Pet(int lifeSpan, String species, String name){
        this.lifeSpan = lifeSpan;
        this.species = species;
        this.name = name;
    }

    public final float costs(float cost){
        return cost;
    }

    public void setSpecies(String species){
        this.species = species;
    }

    public String getSpecies(){
        return this.species;
    }
}
public class Dog extends Pet{
    protected String smell;
    private String species;

    public Dog(String smell){
        super(15, "Dog", "Rex");
        this.smell = smell;
    }

    public Dog(){

    }

    public void setSmell(String smell){
        this.smell = smell;
    }

    public String getSpecies(){
       super.getSpecies();
       smell = "high"; //Meant to deliberately set it to "High". How am I to return this?
    }

    public String getSmell(){
        return this.smell;
    }
}
子类“Dog”:

public class Pet {
    protected int lifeSpan;
    protected  String species, name, interaction;

    public Pet(){
    }

    public Pet(int lifeSpan, String species, String name){
        this.lifeSpan = lifeSpan;
        this.species = species;
        this.name = name;
    }

    public final float costs(float cost){
        return cost;
    }

    public void setSpecies(String species){
        this.species = species;
    }

    public String getSpecies(){
        return this.species;
    }
}
public class Dog extends Pet{
    protected String smell;
    private String species;

    public Dog(String smell){
        super(15, "Dog", "Rex");
        this.smell = smell;
    }

    public Dog(){

    }

    public void setSmell(String smell){
        this.smell = smell;
    }

    public String getSpecies(){
       super.getSpecies();
       smell = "high"; //Meant to deliberately set it to "High". How am I to return this?
    }

    public String getSmell(){
        return this.smell;
    }
}

第一件事:调用
super.getSpecies()
时,您应该将其返回值保存或交给某个地方。然后,您可以考虑将此返回字符串连接为您的第二个返回值(<代码>高< /代码>)如下:

public String getSpecies(){
   return "high " + super.getSpecies();
}
但是:

  • 那只高狗的返回在我看来没有多大意义
  • getter只需要返回一个值,即它的名称来自的值
除了传递将结果作为参数的对象外,没有其他方法可以返回多个值。但这个解决方案远不是一个简单的getter


你应该考虑(在下面的评论中指出),在你的例子中使用两个不同的吸收器,<代码> GET物种()/<代码>和<代码> GETSMELL()/代码>,并将它们的结果组合在你调用它们的地方。

< P>你不能在一个函数中返回两个值。您需要做的是将getter用于嗅觉成员变量

public class Dog extends Pet{
    protected String smell;
    private String species;

    public Dog(String smell){
        super(15, "Dog", "Rex");
        this.smell = smell;
    }

    public Dog(){

    }

    public void setSmell(String smell){
        this.smell = smell;
    }

    public String getSpecies(){
       super.getSpecies();
    }

    public String getSmell(){
        return this.smell;
    }

}
那么假设你想要物种和气味,你必须检查宠物是否真的是狗,如果是,你可以安全地把它当作狗,并使用狗类的特定方法

if ( pet instanceof Dog ) {
    String species = pet.getSpecies();
    String smell = (Dog)pet.getSmell();
}

问题是什么?一个方法不能返回两个值。但是我可以知道为什么不能单独使用
getSnoot()
方法吗?我的问题如下:重写字符串getSpecies()方法,以便同时显示气味变量。因此基本上我必须重写父类中的species方法,并将子类中的私有变量添加到itSo中,我想这意味着
getSpecies()
必须返回与气味相连的物种。这将打印highDog。好的,谢谢,明白了,也许我把问题解释错了:)@Booch你是受欢迎的(也受欢迎的:)。如果他们让您满意,您可以将一个答案标记为已接受,如果他们能为您提供更多帮助,您也可以查看其他答案。再次感谢,我恳请您查看下面的答案,因为这样做是不好的做法?这不是一个好的做法。getter应该返回变量的值,而不是串联的值。啊,谢谢,非常有趣的方法!不过,目前仍在努力坚持先发后发。谢谢:-)我强烈建议不要在一个返回中连接不同的变量。这是一种不好的做法,非常不清楚,而且显然不是
getSpecies()
方法的目的。明白你的意思了。。介意向我解释一下你是如何在我发布的狗类中输入上述条件的吗?好吧,你应该在你想要获取气味和物种的地方使用它。您在第一个程序中放置了整个条件,而不是单行
pet.getSpecies()