子类实例化 |堆栈溢出异常 [英] Subclass instantiation | Stackoverflowexception

查看:60
本文介绍了子类实例化 |堆栈溢出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多代码,但问题本身对我来说很清楚,所以我发布了一个例子:

I got a lot of code, but the problem itself is very clear to me so I'm posting an example:

public class ExcelTable
{
    public ExcelTable() // CONSTRUCTOR
    {
        // create new excel-application and so on..
    }

    public ExcelOutput Output = new ExcelOutput(); // Stackoverflowexception

    private xlWorkbook;  // I need these to inherit into ExcelOutput
    private xlWorksheet; // ''    ''
    // ...               // ''    ''

    public class ExcelOutput : ExcelTable
    {
        public void SaveAs()
        {
            // self explaining
        }

        public void Show()
        {
            // self explaining
        }

        public void Print()
        //....
    }
}

目标:已编辑
为了为 excel 建立一个明确的帮助类,我的愿望是创建一个子类来处理可能的输出.详细地说,我试图防止需要从外部创建 2 个实例 => ExcelTableExcelOutput.我宁愿创建一个单一的实例 ExcelTable 并使用这个实例访问子类.因为需要继承,所以不能将类设置为静态.

Goal: EDITED
In purpose of a clear helping-class for excel my wish is to create a subclass which takes care for the possible output. In detail I'm trying to prevent the need to create 2 instances from outside => ExcelTable and ExcelOutput. Im rather would like to create a single instance ExcelTable and access the subclass with this instance. Because I need the inheritance, I cant set the class to static.

问题:
我得到一个 Stackoverflowexception.我尝试调试我的代码.在 ExcelOutput 实例化时,代码在这一行循环.意思是:即使我尝试单步执行 (F11) 它,执行也会停留在 ExcelOutput 的实例化,这导致我的工具出现 Stackoverflowexception.

Problem:
Im getting a Stackoverflowexception. I tried debugging my code. At the instantiation of ExcelOutputthe code is looping at this single line. Means: Even if I try to Single-Step (F11) it, the execution stays at the instatiation of ExcelOutput which leads my tool to the Stackoverflowexception.

我尝试过的:

public ExcelTable() // CONSTRUCTOR
{
    // create new excel-application and so on..

    Output = Output ?? new ExcelOutput();
}

public ExcelOutput Output;

在这种情况下,会一遍又一遍地创建 ExcelTable.这整个上层阶级永远被建立起来.

In this case, ExcelTable is created over and over again. This whole upper class gets instatiated for ever.

那么谁能向我解释为什么这一行永远循环?看起来 ExcelOutput 的实例化正在创建 ExcelTable 的新实例.

So could anyone explain to me why this single line is looping forever? It looks like the instantiaton of ExcelOutput is creating a new instance of ExcelTable.

我知道这更多是关于知识的,因为我是继承和子类的新手.谷歌在这方面帮不上大忙.根据类、子类和继承,有太多的东西了.

I know this is more about knowledge as Im a newbie in case of inheritance and subclasses. Google wasnt a big help in here. There's just too much stuff out there according classes, subclasses and inheritance.

感谢每一个帮助.非常感谢!

Every help is appreciated. Thanks a lot!

推荐答案

如前所述,问题在于 ExcelOutput 继承自 ExcelTable 从而继承了 使用 ExcelOutput 的新实例初始化的输出 字段,该实例再次具有 Output 字段 ...

As mentioned before, the problem is that ExcelOutput inherits from ExcelTable and thus inherits the Output field which is initialized with a new instance of ExcelOutput which again has an Output field ...

这种递归组合将填充堆栈,从而导致 StackOverflowException 异常.

This recursive composition will fill the stack and thus cause the StackOverflowException exception.

这里有一个快速解决方法,可以帮助您:

Here is a quick fix that will get you going:

public class ExcelTable
{
    public ExcelTable() 
    {
        // create new excel-application and so on..
        //initialize xlWorkbook and xlWorksheet

        Output = new ExcelOutput(xlWorkbook, xlWorksheet);
    }

    public ExcelOutput Output;

    private Workbook xlWorkbook;
    private Worksheet xlWorksheet;

    public class ExcelOutput
    {
        private Workbook xlWorkbook;  
        private Worksheet xlWorksheet;

        public ExcelOutput(Workbook xl_workbook, Worksheet xl_worksheet)
        {
            xlWorkbook = xl_workbook;
            xlWorksheet = xl_worksheet;
        }

        public void SaveAs()
        {
            // self explaining
        }

        public void Show()
        {
            // self explaining
        }

        public void Print()
        {

        }
        //....
    }
}

ExcelOutput 不再继承自 ExcelTable,但它在构建时接收所需的依赖项.

ExcelOutput no longer inherits from ExcelTable, but it receives the required dependencies upon construction.

我的解决方案绝不是一个好的设计.我只是给你一个快速解决方案.一个好的设计需要对大局的理解.

My solution is in no way a good design. I am just giving you a quick fix. A good design would require an understanding of the bigger picture.

这篇关于子类实例化 |堆栈溢出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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