[英]Program stucks on creation of class object in java
我有一个父类“应用程序窗口”和一个子类“ config”。 当我创建“ config”对象时,类执行往往会陷入循环并继续创建该对象。
以下是代码片段:
public class ApplicationWindow implements ActionListener{
public String workSpace;
public String logFile;
public JFrame frmGenericAutomationFramework;
public JProgressBar progressBar;
public File currentTestSuiteFolder;
public String currentTestSuiteName;
config cfg;
SettingsFrame settingsFrame;
TestSuiteFrame testSuiteFrame;
PromptTestSuiteName testSuitePrompt;
public ApplicationWindow (){
initialize();
//**cfg = new config();**
cfg.readProperties();
}
}
子类“ config”如下:
public class config extends ApplicationWindow{
String str;
File cfgfile;
FileOutputStream out;
FileInputStream in;
Properties props;
String filepath = "D:/Webdriverwork/GAF/res/elements.properties";
public config (){
try{
cfgfile = new File(filepath);
in = new FileInputStream(cfgfile);
props = new Properties();
}
catch (Exception e){
// Log message in log file
String message = e.getMessage();
System.out.println(message);
// Exit the system
System.exit(0);
}
}
public void readProperties (){
try{
props.load(in);
workSpace = props.getProperty("WORKSPACE");
logFile = props.getProperty("LOGFILE");
}
catch (Exception e){
// Log message in log file
String message = e.getMessage();
System.out.println(message);
// Exit the system
System.exit(0);
}
}
public void updateProperty (String key, String value){
try{
props.setProperty(key,value);
}
catch (Exception e){
// Log message in log file
String message = e.getMessage();
System.out.println(message);
// Exit the system
System.exit(0);
}
}
public void writeProperties (){
try{
in.close();
out = new FileOutputStream(cfgfile);
props.store(out, null);
out.close();
}
catch (Exception e){
// Log message in log file
String message = e.getMessage();
System.out.println(message);
// Exit the system
System.exit(0);
}
}
}
这就是问题:
public class config extends ApplicationWindow
在ApplicationWindow
构造函数中与此结合:
cfg = new config();
因此,要创建一个新的ApplicationWindow
,您需要创建一个新的config
...但这是一个ApplicationWindow
本身,因此它将创建另一个 config
...进而将创建另一个 config
等。
为什么config
扩展ApplicationWindow
? 在我看来,这听起来像是一个奇怪的设计-配置不是窗口。 只需摆脱extends
规范,您可能会发现其他所有方法都可以使用。
此外,我强烈建议您遵循Java命名约定(例如,使用PascalCase作为类名),并将所有字段设为私有。
Config
对象以循环方式创建。 当您创建Config
对象时, ApplicationWindow
类的参数构造函数不会隐式执行,并且它会再次创建config
Object的对象并重复执行。
记住一件事
即使不调用超类的隐式无参数构造函数,也将始终对其进行调用。
public Config()
{
super(); // compiler adds this statement inside constructor even if you don't declare
}
并且您的超类ApplicationWindow
正在创建另一个Config
对象。
为了避免这种情况,请从ApplicationWindow
类构造函数中删除new Config()
对象创建行,该行将停止循环。 并且您已经创建了Config
对象的引用。
您已经在父类的构造函数中创建了子类对象。 创建子类的对象时,将调用其构造函数,该构造函数首先调用父类构造器,然后执行子类构造器,在此子类对象再次被创建,并且一次又一次地重复。 因此,这将导致递归操作,从而导致无限循环。
config不能扩展ApplicationWindow,因为存在以下情况:AppWindow从config调用构造函数,而config构造函数调用其超级构造函数,即AppWindow-> endless
为什么要使config
从ApplicationWindow
扩展? 您应该删除此继承关系。
您的代码将导致堆栈溢出错误,因为子类的构造函数将在初始化时首先调用其父构造函数。 问题是您的ApplicationWindow
作为config
父类,调用其子类的构造函数,这是意外的,并且会导致无限循环。
如果您打算从ApplicationWindow调用readProperties(),则使其成为抽象方法并删除cfg引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.