本文介绍了StackOverflow混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一名Java新手,在StackOverflow错误/在类之间访问文件的能力方面有一个非常令人困惑的问题。我知道潜在的原因可能是我有一些递归调用,但修复它的语法让我摸不着头脑。我认为这与类如何通过一个扩展另一个扩展链接有关–但是,如果InputScreen类不扩展ViewController,我就不能访问那里我需要的方法。我已经把高级代码放在下面(编写一个跟踪汽油里程的程序)。
这样做的目标是能够打开包含一些历史里程数据的XML文件(使用doOpenAsXML()方法),然后允许用户将数据添加到一些文本字段(在InputScreen类中定义),将另一个数据点添加到ArrayList,然后使用doSaveAsXML方法保存。
有谁有办法让这件事奏效吗?谢谢!
// Simple main just opens a ViewController window public class MpgTracking { public static void main(String[] args) { ViewController cl = new ViewController(); cl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); cl.setVisible(true); } // end main }public class ViewController extends JFrame {
// the array list that I want to fill using the historical data public ArrayList<MpgRecord> hist; public ViewController() { doOpenAsXML(); // open historical data, put into 'hist' InputScreen home = new InputScreen (); } public void doSaveAsXML() { // ...long block to save in correct xml format } public void doOpenAsXML() { // ...long block to open in correct xml format }}
公共类InputScreen扩展了视图控制器{
//定义带有文本字段和‘保存’按钮的屏幕的语句
//在保存按钮上创建监听程序的语句
//要添加到ArrayList历史记录的语句,在ViewController方法中打开
DoSaveAsXML();
)推荐答案
这似乎是问题的根本原因:
但是,如果InputScreen类不扩展ViewController,我就无法访问那里我需要的方法。
要访问另一个类中的(非静态)方法,您需要此类的对象。在您的情况下,InputStream将需要具有一个视图控制器对象,而不是一个视图控制器对象。(在同一行中,视图控制器不应该是JFrame,但应该有JFrame--尽管这在这里不会产生问题。)
如果更改此设置,则不会获得构造函数循环。
public class ViewController { ... public ViewController() { doOpenAsXML(); // open historical data, put into 'hist' InputScreen home = new InputScreen (this); // give myself to our new InputScreen. // do something with home } public void doSaveAsXML() { // ...long block to save in correct xml format } public void doOpenAsXML() { // ...long block to open in correct xml format } } public class InputScreen { private ViewController controller; public InputScreen(ViewController contr) { this.controller = contr; } void someMethod() { // statements to define a screen with text fields and a 'Save' button // statements to create a listener on the Save button // statements to add to the ArrayList hist, opened in the ViewController method controller.doSaveAsXML(); } }这篇关于StackOverflow混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,






