从导入的子类访问Java父方法 [英] java Access parent method from imported child class

查看:87
本文介绍了从导入的子类访问Java父方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是Java新手.从网上学习.我遇到了代码问题. 我有三堂课. mainclass.java,oneclass.java,twoclass.java. 我在mainclass中导入了oneclass和twoclass.

Am a java newbie. Learning from the web. I am stuck with an issue with my code. I have three class. mainclass.java, oneclass.java, twoclass.java. I imported oneclass and twoclass in mainclass.

oneclass具有下一步"按钮.我想做的是在下一个btn上的onActionEvent,从mainclass调用一个函数,该函数会将oneclass的可见性设置为false,twotwoclass的可见性设置为true. 我正在发布整个代码 mainclass.java

oneclass has a "next" button. What i want to do is onActionEvent on next btn , call a function from mainclass, which would set the visibility of oneclass to false and twoclass to true. Am posting the whole code for mainclass.java

    package com.mainclass;
    import com.twoframe.twojframes;
    import com.secondframe.secondjframe;

public class MainClass {
    private static com.secondframe.twoclass panel2;
    private static com.twoframe.oneclass panel1;


    private static void openPanel1(){
      panel1 = new com.twoframe.oneclass();
      panel1.setVisible(true);
    }
    public static void toggleVisibility(){
        System.out.println("called from child");
        panel2 = new com.secondframe.twoclass();
        panel2.setVisible(true);
        panel1.setVisible(false);
    }
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                openPanel1();
            }
        });

    }
}

oneclass.java

oneclass.java

jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
        {                                         
            // TODO add your handling code here:
           //Basically something like -- mainclass.toggleVisibility();

        } 

谢谢.

推荐答案

假设父级"是您要扩展的类,并且您调用的方法不是静态的,则应使用以下方法:

Assuming the "parent" is a class you're extending and the method you're calling is NOT static, the following should do the trick:

super.toggleVisibility();

如果它是静态方法-甚至更简单:

If it's a static method - it's even Simpler:

ParentClassName.toggleVisibility();

这篇关于从导入的子类访问Java父方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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