本文介绍了编写一个通用类来处理内置类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

并不是很实用,也许,但仍有趣。

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.

在这里,我注意到,刚才INT矩阵不是很好,如果我偶尔想使用它与小数或双。当然,我的可以的尝试只是为了施展全部翻番,但是这不是方便的方法。

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.

继续假设,我们可以有一大堆的对象,我们可以添加和繁殖 - 为什么不使用他们在我的基质

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?

所以,只是在考虑这将是一个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.

和问题是我可以覆盖运营商在我的课,但我不能提出将支持运营商的接口。我有一个运营商内置类型,但仍然没有接口他们。

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.

感谢。

推荐答案

为此,您需要通用的数学。幸运的是我已经做到了这一点。用法是类似这样的复杂(即X + IY)例如。操作员类现在 MiscUtil

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.

这篇关于编写一个通用类来处理内置类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:40