引用抽象类内的实例变量

huangapple go评论91阅读模式
英文:

Referencing instance variables from within abstract class

问题

以下是您要翻译的内容:

我有一个抽象类,其中规定了每个子类应该实现的内容。大多数方法对于每个子类都有不同的实现,但有些方法对所有子类都是相同的。我希望这些相同的方法在抽象类中定义,这样我就不必将相同的方法粘贴到几个不同的类中。然而,当我在抽象类的子类实例上调用该方法时,我会收到空指针异常,我想这是因为在抽象类中实现的方法引用了抽象类的字段,而不是实例的字段。

有人可以指出问题出在哪里吗?

例如:

abstract class ControlView {
    String[] controls;

    abstract void render();

    void release() {
        for (int i = 0; i <= controls.length; i++) {
            //Release the controls
        }
    }
}

class StartingControls extends ControlView{

    String[] controls;
    Button uiDrawButton;
    Button uiLoadButton;

    StartingControls() {
        this.controls = new String[2];

        uiDrawButton = new Button();
        this.controls[0] = uiDrawButton;

        uiLoadButton = new Button();
        this.controls[1] = uiLoadButton;
    }

    public void render() {
        //Unique Render implementation
    } 
}

当我调用:

instanceOfStartingControls.release();

我显然希望遍历我在构造instanceOfStartingControls时放入其controls字段中的两个字符串。我不想遍历似乎存在于抽象类中的未初始化数组。

这是一些访问修饰符或静态方法的组合使得它无法按照看起来应该的方式工作,还是我对抽象类的一些关键知识理解不够?这似乎是一个基本问题,但是我很难用言语表达出来,因此我对搜索结果不满意。

英文:

I have an abstract class which lays out what each of its subclasses should implement. Most methods will have different implementations for each subclass, but some will be the same for all. I want the methods that will be the same to be to be defined in the abstract class, so that I'm not pasting the same method into several different classes. However, when I call that method on an instance of the abstract's subclass, I receive a nullpointer because, I imagine, the method implemented in the abstract class is referencing the abstract's field, not the instance's field.

Can someone point out where the flaw is?

For example:

abstract class ControlView {
    String[] controls;

    abstract void render();

    void release() {
        for (int i = 0; i &lt;= controls.length; i++) {
            //Release the controls
        }
    }
}



class StartingControls extends ControlView{

    String[] controls;
    Button uiDrawButton;
    Button uiLoadButton;


    StartingControls() {
        this.controls = new String[2];

        uiDrawButton = new Button();
        this.controls[0] = uiDrawButton;

        uiLoadButton = new Button();
        this.controls[1] = uiLoadButton;
    }


    public void render() {
        //Unique Render implementation
    
    } 
}

When I call

instanceOfStartingControls.release();

I obviously want to iterate over the two strings that I put into instanceOfStartingControls' controls field when it was constructed. I do not want to iterate over the non-initialized array that is apparently living in the abstract.

Is it some combination of access modifiers or static methods that is keeping this from working as it seems it should, or am I missing some crucial bit of knowledge on abstract classes? This feels like a basic question, but I'm having a hard time putting it to words, so I've not been satisfied with any results from my searches.

答案1

得分: 1

有两个名为 controls 的数组。派生类中的数组遮盖了基类中的数组,因此基类实例永远不会被设置为非空。

从派生类中删除这个声明。

这个问题与基类是否为 abstract 无关。如果在派生类中使用与基类中使用的字段名相同的字段名,基类实例将被遮盖。

英文:

There are two arrays called controls. The one in the derived class is obscuring the one in the base class, thus the base instance never gets set non-null.

Delete the declaration from the derived class.

This issue is not related to the base being abstract. If you use the same field name in a derived class as is used in a base class, the base instance will be obscured.

huangapple
  • 本文由 发表于 2020年10月14日 06:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/64344071.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定