编写泛型类来处理内置类型

时间:2023-01-15 19:00:29

Not too practical maybe, but still interesting.

可能不太实用,但仍然很有趣。

Having some abstract question on matrix multiplication I have quickly implemented a matrix for ints, then tested my assumptions.

有一些关于矩阵乘法的抽象问题我已经快速实现了一个用于整数的矩阵,然后测试了我的假设。

And here I noticed that just int matrix is not good, if I occasionally want to use it with decimal or double. Of course, I could try just to cast all to double, but that's not convenient way.

在这里我注意到只有int矩阵不好,如果我偶尔想用十进制或双精度。当然,我可以尝试将所有人都加倍,但这不方便。

Continue with assumption we could have a bunch of objects we are able to add and multiply - why don't use them in my matrix?

继续假设我们可以有一堆对象,我们可以添加和繁殖 - 为什么不在我的矩阵中使用它们?

So, just after considering it would be a Matrix class now I faced that generic T could not be used, I need it to support some interface which could add and multiply.

所以,在考虑它将是一个Matrix类之后,我现在面临着无法使用泛型T,我需要它来支持一些可以添加和增加的接口。

And the problem is I could override operators in my class, but I could not introduce an interface which would support operators. And I have an operators in built-in types, but still no interface over them.

问题是我可以覆盖我的类中的运算符,但我无法引入支持运算符的接口。我有一个内置类型的运算符,但仍然没有接口。

What would you do in such a case considering you do not want to duplicate worker class body? Wrappers and implicit casting didn't help me much, I'm interested in a beautiful solution.

考虑到你不想复制工人阶级的身体,你会在这种情况下做什么?包装和隐式铸造对我没什么帮助,我对一个漂亮的解决方案很感兴趣。

Thanks.

2 个解决方案

#1


5  

For this you need generic maths. Luckily I have done this. Usage would be similar to this "complex" (i.e. x+iy) example. The Operator class is now part of MiscUtil.

为此,您需要通用数学。幸运的是我做到了这一点。用法类似于这个“复杂”(即x + iy)示例。 Operator类现在是MiscUtil的一部分。

#2


2  

Well, there is a less tech-heavy way to do just that. You cannot add a new interface for "int" or "double". But you can declare an interface for an object that can multiply and add values of some generic type. And then you can implement the interface for all the types you need:

那么,有一种技术较少的方法可以做到这一点。您无法为“int”或“double”添加新接口。但是,您可以声明一个对象的接口,该接口可以相乘并添加某些泛型类型的值。然后,您可以为所需的所有类型实现接口:

public interface ICalculator<T>
{

   T Add(T x, T y);
   T Multiply(T x, T y);

}

public class MatrixMultiplier<T>
{

  public MatrixMultiplier(ICalculator<T> calculator) { ... }

}

public class IntCalculator : ICalculator<int>
{

  public int Add(int x, int y)
  {
    return x + y;
  }

  public int Multiply(int x, int y)
  {
    return x * y;
  }

}

#1


5  

For this you need generic maths. Luckily I have done this. Usage would be similar to this "complex" (i.e. x+iy) example. The Operator class is now part of MiscUtil.

为此,您需要通用数学。幸运的是我做到了这一点。用法类似于这个“复杂”(即x + iy)示例。 Operator类现在是MiscUtil的一部分。

#2


2  

Well, there is a less tech-heavy way to do just that. You cannot add a new interface for "int" or "double". But you can declare an interface for an object that can multiply and add values of some generic type. And then you can implement the interface for all the types you need:

那么,有一种技术较少的方法可以做到这一点。您无法为“int”或“double”添加新接口。但是,您可以声明一个对象的接口,该接口可以相乘并添加某些泛型类型的值。然后,您可以为所需的所有类型实现接口:

public interface ICalculator<T>
{

   T Add(T x, T y);
   T Multiply(T x, T y);

}

public class MatrixMultiplier<T>
{

  public MatrixMultiplier(ICalculator<T> calculator) { ... }

}

public class IntCalculator : ICalculator<int>
{

  public int Add(int x, int y)
  {
    return x + y;
  }

  public int Multiply(int x, int y)
  {
    return x * y;
  }

}