根据if else(或switch)c#声明不同对象类型的2D数组

问题描述 投票:0回答:1

我正在使用矩阵并且有问题如何正确地初始化不同的2D对象数组,当它们的类型取决于条件(如果否则)。如果我在if之前声明矩阵及其类型,那么我不能在里面声明不同的类型。如果我在if else中声明它,它不存在于范围之外。

在Stack Overflow上已经有类似的问题,我找到了一些方法来解决这个问题。

  1. if else中的所有方法(即使没有重载 - 对两种类型都完全相同)。 - >这有效但代码重复。
  2. 制作通用界面。 - >方法不适用于ICell,我无法弄清楚将ICell [] []重新输入CellA [] []。
  3. 将矩阵声明为变量数组的数组。 - >无法弄清楚这是如何工作的。

还有其他选择,什么是最佳解决方案?或者我的做法完全错了?

谢谢

附:代码很长,这是简化版。

class CellA : IComparable {
    // 2 attributes
    //constructor 1 param
    public int CompareTo(object obj) {
        //code
    }
}

class CellB : CellA {  
    // 3 attributes  
    //constructor 2 params  
}

class Program {    
    static void Main(string[] args) {
        data[0] = "...";
        ...
        data[x] = "...";
        //user input own data or chooses data set
        ...
        bool mode = true/false; //user chooses computing mode
        if (mode) {
            CellA[][] matrix = InitializeMatrixA(data[indexOfSet]);
        } else {
            CellB[][] matrix = InitializeMatrixB(data[indexOfSet]);
        }
        DoSomethingOther(ref matrix);
        //several ref matrix manipulation methods
        Console.WriteLine(DoSomethingSame(matrix));
    }

    static CellA[][] InitializeMatrixA(string data) {
        //string processing, not important
        CellA[][] matrix = new CellA[9][];
        for (int i = 0; i < 9; i++) {
            matrix[i] = new Cell[9];
            for (int j = 0; j < 9; j++) {
                matrix[i][j] = new CellA(stringPart[i*9+j]);
            }
        }
        return matrix;
    }

    static CellB[][] InitializeMatrixB(string data) {
        //different string processing, not important
        CellB[][] matrix = new CellB[9][];
        for (int i = 0; i < 9; i++) {
            matrix[i] = new Cell[9];
            for (int j = 0; j < 9; j++) {
                matrix[i][j] = new CellA(stringPart[i*18+j*2], stringPart[i*18+j*2+1]);
            }
        }
        return matrix;
    }
    //same function for As and Bs
    static int DoSomethingSame(ref CellA[][] matrix) { //code }

    //many different overloaded methods all working with reference to "matrix", slightly different computing for As and Bs
    static void DoSomethingOther(ref CellA[][] matrix) { //code }
    static void DoSomethingOther(ref CellB[][] matrix) { // slightly different code}
c# class object multidimensional-array types
1个回答
0
投票

嗯,在我看来,你发布的第二个解决方案是最好的解决方案,一个叫做ICell的通用接口。

我处理它的方法是像这样创建ICell:不需要ref修饰符,数组自然通过引用传递。

public interface ICell
{
    int DoSomethingSame(ICell[][] matrix);
    void DoSomethingOther(ICell[][] matrix);
}

然后我将创建类:CellA,CellB并使每个实现ICell接口由他们自己的逻辑。单元类的每个构造函数都通过自己的逻辑定义InitializeMatrix的逻辑。因此,当您创建类的实例时,它已经初始化了矩阵。

然后在主要:

static void Main(string[] args)
{
    data[0] = "...";
    ...
    data[x] = "...";
    //user input own data or chooses data set
    ...
    bool mode = true/false; //user chooses computing mode
    ICell[][] matrix = (mode)? new CellA(data[indexOfSet]): new CellB(data[indexOfSet])

    DoSomethingOther(ref matrix);
    //several ref matrix manipulation methods
    Console.WriteLine(DoSomethingSame(matrix));
}

如果您需要示例如何定义CellA和CellB让我知道,我将更新。

编辑:完整的解决方案是:

public class Matrix
{
    ICell[][] cell;
    public Matrix(bool mode, string data)
    {
        cell = (mode)? new CellA(data): new CellB(data);
    }
}

我会在类中实现doSomething方法。

然后在主要:

static void Main(string[] args)
{
    data[0] = "...";
    ...
    data[x] = "...";
    //user input own data or chooses data set
    ...
    bool mode = true/false; //user chooses computing mode
    Matrix matrix = new Matrix(mode, data[indexOfSet]);

    matrix.DoSomethingOther();
    //several ref matrix manipulation methods
    Console.WriteLine(matrix.DoSomethingSame);

}

© www.soinside.com 2019 - 2024. All rights reserved.