简体   繁体   English

引用两个类时,出现StackOverflow错误。 为什么?

[英]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. 您正在进行无限递归。

  • Begin extends Setup, 开始扩展安装程序,
  • and so Begin's constructor must call Setup's constructor, and initialize it's super Setup properties 因此Begin的构造函数必须调用Setup的构造函数,并初始化它的超级 Setup属性
  • and Setup creates internally another Begin instance 然后安装程序在内部创建另一个 Begin实例
  • which will initialize another super Setup instance 这将初始化另一个超级安装程序实例
  • which creates another Begin instance 这将创建另一个 Begin实例
  • which will initialize another super Setup instance 这将初始化另一个超级安装程序实例
  • which creates another Begin instance 这将创建另一个 Begin实例
  • which will initialize another super Setup instance 这将初始化另一个超级安装程序实例
  • which creates another Begin instance 这将创建另一个 Begin实例
  • ... etc until you run out of memory ...等等,直到内存用完

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.

相关问题 为什么会出现Stackoverflow错误? - Why do I get a Stackoverflow Error? 为什么我会收到此递归 stackoverflow 错误? - Why do I get this recursion stackoverflow error? 当我使用“n--”而不是“n-1”时,为什么会出现 stackoverflow 错误? - When I use "n--" instead of "n-1" why do I get stackoverflow error? 当我运行此代码时,我得到stackOverflow错误 - When I run this code I get the stackOverflow error 为什么即使使用@JsonIgnoreProperties,在使用杰克逊时也会出现stackoverflow错误 - Why do i get an stackoverflow error when using jackson even though using @JsonIgnoreProperties 实例变量的值在两个类中是不同的。 为什么? (更多问题) - values of an instance variable is different in two classes. Why? (+ more questions) 我在外部类中创建新实例时出现StackOverflow错误 - I get StackOverflow error when I create new instance in external class Hibernate不会创建Java类。 我能做什么? - Hibernate does not create Java classes. What can I do? 如何引用新的Thread类实例? - How do I refrence new Instances of Thread classes? 为什么我得到NoClassDefFoundError异常而不是StackOverflow错误? - Why am I getting a NoClassDefFoundError exception rather than a StackOverflow error?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM