【问题标题】:Declare 2D array of different object types depending on if else (or switch) c#根据if else(或switch)c#声明不同对象类型的二维数组
【发布时间】:2019-07-07 22:34:55
【问题描述】:

我正在使用矩阵并且有问题如何正确初始化不同的二维对象数组,当它们的类型取决于条件(如果其他)。如果我在 if else 之前声明了矩阵及其类型,那么我不能在里面声明不同的类型。如果我在 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}

【问题讨论】:

  • 您对DoSomethingOther(...)CellB 的“代码略有不同”评论是否表明存在通用接口?也就是说,您的CellB 派生自CellA,因此矩阵可以只是CellA[][],但填充有CellACellB
  • 在计算中使用2个或3个参数是不同的,但有些部分是完全一样的。
  • 但是是的,该方法是否可以只有一个,并且在 if (type is CellA) {} else {}

标签: c# class object multidimensional-array types


【解决方案1】:

嗯,在我看来,您发布的第二个解决方案将是最好的解决方案,一个名为 ICell 的通用接口。

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

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

然后我会创建类:CellA、CellB,并让每个类都按照自己的逻辑实现 ICell 接口。 cell 类的每个构造函数都通过自己的逻辑定义了 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);

}

【讨论】:

  • 从返回 CellA[][] 或 CellB[][] 的方法分配给 ICell[][] 时遇到问题。我有 ICell[][] 矩阵 = null;矩阵 = InitializeMatrixA(...);下一个方法需要 CellA[][] 而不是 ICell[][]。
  • 更好的方法是创建一个带有参数 ICell[][] 单元格的矩阵类。
  • 这应该可以了,现在我只需要弄清楚将 ICell[][] 重新输入到 CellA[][] 或 CellB[][]。
  • 只有细节,矩阵类构造函数应该指定fors,它会很完美。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-20
  • 2016-12-09
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多