[英]I get a StackOverflow Error when I refrence two classes. Why?
I am sorry for the poor title, but I was not sure what my problem is called. 很抱歉,我的头衔很差,但是我不确定我叫什么问题。 (Please let me know in the answer if you can) I have two classes in which I am trying to interact with. (如果可以的话,请在回答中让我知道)我正在尝试与两个类进行交互。 Class 'Setup' has variables I need in class 'Begin', and vice versa. 类“设置”具有类“开始”中需要的变量,反之亦然。 I have tried extending class 'Setup' in the 'Begin' class by doing: 我尝试通过执行以下操作来扩展'Begin'类中的'Setup'类:
public class Begin extends Setup {
Something to note, I do reference the 'Begin' class in the 'Setup' class in this manner: 需要注意的是,我确实以这种方式在“设置”类中引用了“开始”类:
Begin b = new Begin();
I have also tried referencing both the 'Setup' and 'Begin' class in each respective class. 我也尝试在每个类中同时引用'Setup'和'Begin'类。 This did not help. 这没有帮助。 I am getting the 'java.lang.StackOverflowError' error in both classes. 我在两个类中都收到“ java.lang.StackOverflowError”错误。 I get the error where I refrence the other class. 我在改读其他班级时遇到错误。 (Error in 'Begin': 'Setup s = new Setup();' Error in 'Setup': 'Begin b = new Begin();') (“开始”中的错误:“设置s =新的Setup();”“设置”中的错误:“开始b =新的Begin();”)
I am not aware of why I am getting this error, and I did not know what 'referencing' a class was properly called. 我不知道为什么会收到此错误,并且我不知道正确调用了一个类的“引用”。 I apologize if this is a duplicate. 如果这是重复的,我深表歉意。 If it is, please link me to the page. 如果是的话,请将我链接到该页面。 If not, please let me know what I can do to get my program working properly again. 如果没有,请告诉我该如何使我的程序再次正常运行。 The code and classes I mentioned in this topic are just examples. 我在本主题中提到的代码和类仅是示例。 They are not the classes I am working with. 他们不是我正在使用的课程。 I thought it may be helpful to note that I am working with JFrame. 我认为注意到我正在使用JFrame可能会有所帮助。 I am only extending one class to JFrame, but both classes are working with JFrame. 我只将一个类扩展到JFrame,但是两个类都在使用JFrame。 I have created a new JFrame in the class that is not extending JFrame though. 我在不扩展JFrame的类中创建了一个新的JFrame。 If you are not able to give an answer you think will help, I will post the code my two classes. 如果您无法给出答案,您认为会有所帮助,我会将代码发布到我的两个班级中。 Thanks in advance! 提前致谢! ~Rane 〜雷恩
EDIT: Thanks guys! 编辑:谢谢大家! I am really glad you were able to help me out with the example I gave. 我很高兴您能够为我提供的示例帮助我。 Thanks again! 再次感谢!
You've got infinite recursion going on. 您正在进行无限递归。
Don't do that. 不要那样做 You need to change the entire structure of your program since a super class should have no knowledge of its child classes much less instances of it. 您需要更改程序的整个结构,因为超类应该对其子类一无所知,其实例要少得多。 My bet is that Begin should not extend Setup, and that's where I'd start. 我敢打赌,Begin不应扩展安装程序,这就是我要开始的地方。 In fact this situation: 实际上这种情况:
Class 'Setup' has variables I need in class 'Begin', and vice versa. 类“设置”具有类“开始”中需要的变量,反之亦然。
is not solved by inheritance but by composition. 不是通过继承而是通过组合来解决。 For example... 例如...
public class Begin {
private Setup setup;
public void setSetup(Setup setup) {
this.setup = setup;
}
and maybe also 也许也
public class Setup {
private Begin begin;
public void setBegin(Begin begin) {
this.begin = begin;
}
Then elsewhere: 然后在其他地方:
Begin begin = new Begin();
Setup setup = new Setup();
begin.setSetup(setup);
setup.setBegin(begin);
There are other ways of injecting dependency and the specifics will depend on your need, so don't take this as gospel, other than don't use inheritance for this purpose. 还有其他注入依赖项的方法,具体取决于您的需要,因此不要将此视为福音,除非不要为此目的使用继承。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.