无法从超类访问变量(构造函数问题) [英] Cannot access variables from superclass(constructor issues)

查看:138
本文介绍了无法从超类访问变量(构造函数问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好。



我是新手,我真的希望你们能教我一两件事。



我只想从超类访问某些变量的值。但是我的构造函数给了我一个问题:



Hello.

Im a newbie and i really hope you guys can teach me a thing or two.

I just want to access, values of certain variables, from the superclass. But my constructor is giving me issue:

public class Calculator extends SwingFrame {
 
    SwingFrame testingobj = null;
 
    public Calculator(SwingFrame frame) {
        this.testingobj = frame;
    }
     
    public int test = 0;
     
    void someMethod(){
        //method
    }
 
}





SwingFrame类:



The SwingFrame Class:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
    Calculator callthemethod = new Calculator(this); 
    callthemethod.someMethod();





这完美无缺。每次按下JButton,它都会调用该方法。



以下是问题:







This works perfectly. It will call the method, every time i press the JButton.

Here is the issue:


public class Mastering extends Calculator {
 
    public Mastering(SwingFrame frame) {
        super(frame);
    }
        
}





所以现在我的Mastering类是Calculator的子类,将继承构造函数。



但是如果我想访问Calculator类中的变量怎么办?



我试过:





So now my Mastering class is subclass of Calculator and will inherit the constructor.

But what if i want to access, variables from the Calculator class?

I tryed:

Calculator test = new Calculator();





它告诉我,类:计算器类计算器,不能应用于给定类型:


需要
:SwingFrame。



现在,我不知道如何从Calculator类调用变量(Calculator test = new Calculator();是唯一的方法,我知道如何从其他类中访问变量。)



请帮助我,我一直在寻找几小时和几天的解决方案。但我卡住了。



非常感谢。



It tells me, that: Calculator in class Calculator, cannot be applied to given types:

required: SwingFrame.

And now, i dont know how to call variables, from the Calculator class(the Calculator test = new Calculator(); is the only way, i know how to access variables from other the class).

Please help me, i just kept looking for solutions for hours and days. But im stuck.

Thank you very much.

推荐答案

因为你继承了计算器类,您还继承了其所有公共变量和受保护变量。因此,您可以在班级内的任何位置直接按名称访问它们。请参阅本教程 [ ^ ]以更全面地描述联合规则。
Since you have inherited from the Calculator class, you have also inherited all its public and protected variables. So, you can access them directly by name, anywhere inside your class. See this tutorial[^] for a fuller description of the rules on i nheritance.


这篇关于无法从超类访问变量(构造函数问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆