在 Java 中扩展 Arrays 类
我正在尝试向数组类添加功能。在我的项目中,我使用数组中的(静态)方法,并且还有一些其他方法也可以处理数组转换、排序等...
我试图将这些方法添加到扩展数组的对象 MyArrays 中,这样我就可以走了,
MyArrays.toList(foo);
但也
MyArrays.myOwnFunction(bar);
可以但我无法扩展它,因为数组构造函数具有私有访问权限。 我知道这并不是真的必要,但现在我知道我做不到,我真的很想做。 有什么解决方法吗?
谢谢,
I'm trying to add functionality to the Arrays-class. In my project I use the (static) methods from Arrays and have some other methods that also handle array-conversion, sorting, etc...
I'm trying to add these to an object MyArrays that extends Arrays so I can go
MyArrays.toList(foo);
but also
MyArrays.myOwnFunction(bar);
but i'm not able to extend it because Arrays-contructor has private access.
I know it's not really necessary, but now I know i'm unable to do it, I realy want to.
Is there any workaround for this?
thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。为什么效用函数是在一个类中还是在两个类中很重要?郑重声明 - commons-lang 的 ArrayUtils 具有其他实用方法。
从技术上讲,您可以通过简单地调用 Arrays 中的相应方法来重新实现实用程序类中的所有方法,但这是不必要的。
You can't. And why would it matter whether you have the utility functions in one or in two classes? For the record - there is commons-lang's
ArrayUtils
that has additional utility methods.Technically, you can reimplement all methods in your utility class by simply calling the corresponding methods in
Arrays
, but that's unneeded.我建议您尝试使用 Trove4j 库。它具有支持原语的集合,添加了许多有用的数组/集合方法,例如 sort()。它们也可以被扩展。
I suggest you try using the Trove4j library. This has collections which support primitives has many of the useful Arrays/Collections methods added, like sort(). They can also be extended.