访问超类中方法的变量

时间:2014-10-12 20:29:21

标签: java class variables

我有一个带有Animal类和Dog和Cat子类的基本代码。我有说话的方法。 speak方法接收一个字符串,并以cat和dog"语言"返回一个字符串。如果一个字符的ascii代码是偶数,则返回" uff"如果没有," vau"。当我覆盖该方法时,我想从Dog类中设置oddSound和evenSound,但我找不到合适的方法来执行此操作。
此代码来自Animal类:

public String speak(String what){
    String speakableString = new String();
    String oddSound = new String();
    String evenSound = new String();

    for (int i = 0; i < what.length(); i++) {
        if((((int) what.charAt(i)) & 1) == 1){ 
            speakableString.concat(oddSound); 
        }else if ((((int) what.charAt(i)) & 1) == 0){
            speakableString.concat(evenSound);
        }
    }

    speakableString = speakableString.substring(0, speakableString.length()-1);
    return speakableString;
}

此代码来自Dog类:

public String speak(String what){
    //set oddSound = "vau"
    //set evenSound = "uff"
    return super.speak(what);
}

4 个答案:

答案 0 :(得分:1)

Animal课程中,有两个受保护的字段

protected String oddSound;
protected String evenSound;

然后,在DogCat类中,您可以设置以下字段:

oddSound = "woof";
evenSound = "woofwoof"

然后,在speak()方法中,您只需使用this.oddSoundthis.evenSound

答案 1 :(得分:0)

Animal类应该声明你的说话方法,因为这对Dog和Cat类来说都很常见。

public class Animal {
    String oddSound;
    String evenSound;

    public Animal(String oddSound, String evenSound) {
        this.oddSound = oddSound;
        this.evenSound = evenSound;
    }

    public String speak(String what){
        String speakableString = new String();

        for (int i = 0; i < what.length(); i++) {
            if((((int) what.charAt(i)) & 1) == 1){ 
                speakableString = speakableString.concat(oddSound); 
            }else if ((((int) what.charAt(i)) & 1) == 0){
                speakableString = speakableString.concat(evenSound);
            }
        }

    speakableString = speakableString.substring(0, speakableString.length()-1);
    return speakableString;
    }
}

请记住string.concat方法创建一个 new 对象,它不会修改它所调用的实例。见String API Documentation

您的Dog和Cat类应仅在需要时定义或覆盖方法。看来你在他们说话方法中所做的就是调用超级方法实现。你可以完全摆脱它。

如果要扩展Animal类以提供新方法或实现,请执行以下操作:

public class Dog extends Animal {
    public Dog() {
        // This calls the super constructor, which sets the oddSound and evenSound fields
        super("vau", "uff");
    }

    // This section does nothing.
    // A method implementation which only calls its super implementation is ineffective.
    // If you were to provide a new implementation, this is where it would be.
    //public String speak(String what) {
    //  super.speak(what);  
    //}
}

答案 2 :(得分:0)

您可以将它们保存为数据成员,并将它们设置在相应的构造函数中:

public class Animal {
    private String oddSound;
    private String evenSound;

    protected Animal (String oddSound, String evenSound) {
        this.oddSound = oddSound;
        this.evenSound = evenSound;
    }

    public String speak(String what){
        String speakableString = new String();

        for (int i = 0; i < what.length(); i++) {
            if((((int) what.charAt(i)) & 1) == 1){ 
                speakableString = speakableString.concat(oddSound); 
            }else if ((((int) what.charAt(i)) & 1) == 0){
                speakableString = speakableString.concat(evenSound);
            }
        }
}

public class Dog extends Animal {
    public Dog() {
        super ("uff", "vau");
    }
}

答案 3 :(得分:-1)

您可以将发言实施分为两部分,以便传入要用于oddSoundevenSound的字符串。

public String speak(String what){
    return getSpeakableString(what, new String(), new String());
}
protected String getSpeakableString(String what, String oddSound, String evenSound){
    //this is just copied from what you had in your question, it likely doesn't do what you actually want.
    String speakableString = new String();
    for (int i = 0; i < what.length(); i++) {
        if((((int) what.charAt(i)) & 1) == 1){ 
            speakableString.concat(oddSound); 
        }else if ((((int) what.charAt(i)) & 1) == 0){
            speakableString.concat(evenSound);
        }
    }
    speakableString = speakableString.substring(0, speakableString.length()-1);
    return speakableString;
}

//in subclass
@Override
public String speak(String what){
    return getSpeakableString(what, "vau", "uff");
}