简体   繁体   English

访问超类中方法的变量

[英]Accessing variable of a method in superclass

I have a basic code with an Animal class and a Dog and a Cat subclass. 我有一个基本代码,包含Animal类和Dog和Cat子类。 I have a speak method. 我有说话的方法。 The speak method receives a string, and returns a string in cat and dog "language". speak方法接收一个字符串,并以猫和狗“语言”返回一个字符串。 If a character's ascii code is even, it returns "uff", if not, "vau". 如果一个字符的ascii代码是偶数,则返回“uff”,如果不是,则返回“vau”。 When I override the method, I want to set oddSound and evenSound from the Dog class, but I can't find a proper way to do this. 当我重写方法时,我想从Dog类中设置oddSound和evenSound,但我找不到合适的方法来执行此操作。
This code is from the Animal class: 此代码来自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;
}

This code is from the Dog class: 此代码来自Dog类:

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

In the Animal class, have two protected fields, Animal类中,有两个受保护的字段,

protected String oddSound;
protected String evenSound;

Then, in the Dog and Cat classes, you can set these fields: 然后,在DogCat类中,您可以设置以下字段:

oddSound = "woof";
evenSound = "woofwoof"

Then, in the speak() method, you can simply use this.oddSound and this.evenSound 然后,在speak()方法中,您可以简单地使用this.oddSoundthis.evenSound

The Animal class should declare your speak method, since that is common to both the Dog and Cat classes. 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;
    }
}

Keep in mind that the string.concat method creates a new object, it does not modify the instance on which it's called. 请记住,string.concat方法创建一个对象,它不会修改调用它的实例。 See String API Documentation 请参阅String API文档

Your Dog and Cat classes should define or override methods only as needed. 您的Dog和Cat类应仅在需要时定义或覆盖方法。 It seems all you're doing in their speak method is calling the super method implementation. 看来你在他们的发言方法中所做的就是调用超级方法实现。 You can get rid of that completely. 你可以完全摆脱它。

If you want to extend the Animal class to provide new methods or implementations, do it like this: 如果要扩展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);  
    //}
}

You can keep them as data members, and set them in the respective constructors: 您可以将它们保存为数据成员,并将它们设置在相应的构造函数中:

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");
    }
}

You could break up your speak implementation in to two parts to allow you to pass in the Strings that you want to use for oddSound and evenSound . 您可以将说话实现分成两部分,以允许您传入要用于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");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM