简体   繁体   English

java继承:在父方法中编写使用子变量的代码

[英]java inheritance: in the parent method write code that uses child variable

I am trying to write a general GUI class that contains main methods used by all different and more specific GUIs. 我正在尝试编写一个通用的GUI类,其中包含所有不同且更具体的GUI使用的主要方法。

public class GeneralGUI extends javax.swing.JFrame {
    protected javax.swing.JTextArea logTextArea;
    protected void say(String s) {
        logTextArea.append(s + "\n");
    }
}

the subclass class contains: 子类类包含:

public class SpecificGUI extends GeneralGUI {
    protected javax.swing.JTextArea logTextArea;
    [...]
    private void initComponents() {
        [...]
        logTextArea = new javax.swing.JTextArea();
    }
}

what I want is that when I call 我想要的是当我打电话时

SpecificGUI myGUI =  new SpecificGUI();
myGUI.say("Ciao!");

the "Ciao!" "Ciao!" string is appended to myGUI.logTextArea , but this doesn't happen: it insteads throws a Null Pointer Exc because it is looking for the logTextArea belonging to GeneralGUI - an object that hasn't been instantiated. 将字符串附加到myGUI.logTextArea ,但这不会发生:它而是抛出一个Null指针Exc,因为它正在查找属于logTextArea一个尚未实例化的对象。

1) why is it so? 1)为什么会这样? 2) how can I achieve the desider result? 2)如何获得目标结果?

thanks! 谢谢!

EDIT: I have tried removing the variable declaration in the subclass: being it a GUI I designed through netbeans, netbeans won't let me remove the declaration! 编辑:我试图删除子类中的变量声明:作为通过netbeans设计的GUI,netbeans不会让我删除该声明!

you dont need to define 您不需要定义

protected javax.swing.JTextArea logTextArea

twice. 两次。

defining it as a protected field of parent class means all inhereting classes has access to this field. 将其定义为父类的受保护字段意味着所有inreting类都可以访问此字段。

inheritance works like this: 继承是这样的:

1) You call parent method foo() from child class B. 1)您从子类B调用父方法foo()。

2) if child class has method foo() it is invoked 2)如果子类具有方法foo(),则将其调用

3) if child has no method foo look up the chain of inheritance for a method foo() and invoke it 3)如果child没有方法foo,则查找方法foo()的继承链并调用它

4) say method foo() in parent class A uses field x, it can only access the field x if A or any super class has it. 4)说父类A中的方法foo()使用字段x,只有在A或任何超类拥有它的情况下,它才能访问字段x。

1) You've already answered the question — the say() method of the parent class it is looking for the logTextArea inside the parent class, where it is declared. 1)您已经回答了这个问题—父类的say()方法,它正在父类中声明该对象的logTextArea查找。 Your re-declaration of the field in the child class creates another field, which hides the same-named field from the parent class. 您在子类中对该字段的重新声明将创建另一个字段,该字段对父类隐藏同名字段。

2) Just remove the declaration of another logTextArea in child class. 2)只需删除子类中另一个logTextArea的声明logTextArea

If you really need another field in the child class with the same name, then you can access the (hidden) parent field via super.logTextArea . 如果您确实需要子类中具有相同名称的另一个字段,则可以通过super.logTextArea访问(隐藏)父字段。

1-logTextArea is not getting instantiated thats why you are getting NPE. 1-logTextArea没有得到实例化,这就是为什么要获得NPE的原因。 2-You have to decide where you want logTextArea as class attribute.i think it does not make any sense keeping it in both classes,else try and instantiate logTextArea of generalGUI class. 2-您必须确定要将logTextArea作为类属性的位置。我认为将其保留在两个类中没有任何意义,否则请尝试实例化generalGUI类的logTextArea。

protected javax.swing.JTextArea logTextArea   

hides the member variable of parent class GeneralGUI and you haven't initialize logTextArea with nothing so it remains Null that's why you got NullPointerEception . 隐藏父类GeneralGUI的成员变量,所以它仍然是 ,这就是为什么你有NullPointerEception你还没有初始化没有logTextArea。 You should just remove member variable logTextArea from SpecificGUI class 您应该只从SpecificGUI类中删除成员变量logTextArea

Please remove the protected javax.swing.JTextArea logTextArea in the child class. 请在子类中删除受保护的javax.swing.JTextArea logTextArea。

Next in you parent protected javax.swing.JTextArea logTextArea is not defined and is null, it is a object and perhaps needs to be initiated prior to append. 接下来,在父级受保护的javax.swing.JTextArea中,logTextArea未定义且为null,它是一个对象,可能需要在追加之前启动。 Please check. 请检查。

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

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