Java泛型方法

发布于 2024-10-27 18:43:04 字数 1006 浏览 1 评论 0原文

我有一大堆定义这两个常量的类,例如:

public class Face
{
    public static final int LUMP_INDEX = 1;
    public static final int SIZE = 20;

    blah blah
}

public class Edge
{
    public static final int LUMP_INDEX = 5;
    public static final int SIZE = 32;

    blah blah
} 
etc.

目前,我有一个函数为每个类创建一个该类的数组,使用类中定义的 2 个常量。

private Face[] createFaces(RandomAccessFile in)
{
    int numFaces = doSomeCalculations(Face.LUMP_INDEX, Face.SIZE);
    Face[] faces = new Face[numPlanes];

    blahblah;
    for(int i = 0; i < numFaces; i++)
        faces[i] = new Face();

    return faces;
}

为每个类都有一个创建函数有点愚蠢。唯一改变的是类类型。所以我想创建一个可以与上述任何类一起使用的通用方法。比如:

private T[] create(RandomAccessFile in, Class T)
{
    int num = doSomeCalculations(T.LUMP_INDEX, T.SIZE);
    T[] faces = new T[numPlanes];

    blahblah;
    for(int i = 0; i < num; i++)
        faces[i] = new T();

    return faces;
}

但是我不知道如何正确地做到这一点。任何帮助将不胜感激。谢谢。

I have a whole bunch of classes that define these two constants, eg:

public class Face
{
    public static final int LUMP_INDEX = 1;
    public static final int SIZE = 20;

    blah blah
}

public class Edge
{
    public static final int LUMP_INDEX = 5;
    public static final int SIZE = 32;

    blah blah
} 
etc.

At the moment, i have a function for each one to create an array of that class, using the 2 constants defined in the class.

private Face[] createFaces(RandomAccessFile in)
{
    int numFaces = doSomeCalculations(Face.LUMP_INDEX, Face.SIZE);
    Face[] faces = new Face[numPlanes];

    blahblah;
    for(int i = 0; i < numFaces; i++)
        faces[i] = new Face();

    return faces;
}

A bit silly to have a create function for every class. The only thing that changes is the class type. So i wanted to create a genertic method that would work with any of the classes above. Something like:

private T[] create(RandomAccessFile in, Class T)
{
    int num = doSomeCalculations(T.LUMP_INDEX, T.SIZE);
    T[] faces = new T[numPlanes];

    blahblah;
    for(int i = 0; i < num; i++)
        faces[i] = new T();

    return faces;
}

However I'm not sure how to do it properly. Any help would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蓝梦月影 2024-11-03 18:43:04

唯一可行的方法是使用反射通过类对象读取常量,并且 Array.newInstance() 创建数组。哦,方法签名必须如下所示:

private <T> T[] createFaces(RandomAccessFile in, Class<T> clazz)

编辑
另一种解决方案是将常量保留在按类键控的映射中,而不是作为静态字段。 Java 的动态性不够,尤其是在类级别上,无法按照您的方式干净利落地实现。

The only way that could be made to work is by using reflection to read the constants through the class object and Array.newInstance() to create the array. Oh, and the method signature would have to look like this:

private <T> T[] createFaces(RandomAccessFile in, Class<T> clazz)

Edit
An alternative solution would be to keep the constants in a map keyed by class rather than as static fields. Java just isn't dynamic enough, especially on the class level, to do it your way cleanly.

初见 2024-11-03 18:43:04

在这种情况下没有理由使用反射。抽象工厂或类似工厂中的 Bung。

There's no excuse for using reflection in a situation like this. Bung in an Abstract Factory or similar.

阪姬 2024-11-03 18:43:04

潜在地,你可以通过让你想要创建的两个(全部?)项目扩展一些超类“Array-Able Object”来做到这一点,

但是如果你在 createFaces 中的“blablabla”不一样,你什么也得不到,而且把自己逼到了一个角落与该扩展名。

我会研究你是否真的需要这些不同的类,它们都做同样的事情,或者是否有一些更通用的修复你可以研究。例如,如果两者之间的唯一区别是它们的静态变量,那么您应该将它们移动到一个位置,例如资源文件。

但是,如果您拥有相同的内容就是此代码的“创建数组”部分,那么分离或以某种方式链接它们充其量似乎是不必要的。

Potentially you could do this by having both (all?) of the items you want to create extend some superclass "Array-Able Object"

But provided your "blablabla" in createFaces is not the same, you gain nothing and back yourself into a corner with that extension.

I'd look into whether or not you really need these different classes which all do the same thing, or whether there's some more general fix you could look into. If the only difference between the two is their static variables, for instance- then you should move those to a single location, say- a resource file.

If all you have the same, though, are those "create array" sections of this code, then separating or somehow linking them seems unnecessary at best.

电影里的梦 2024-11-03 18:43:04

你总是可以使用更简单的形式:

<T> T[] create(RandomAccessFile in, Class<T> clazz, int lumpIndex, int size)
{
    int numPlanes = doSomeCalculations(lumpIndex, size);
    T[] faces = new T[numPlanes];

    blahblah;

    return faces;
}

并像使用它一样

Face[] faces = create(in, Faces.class, Face.LUMP_INDEX, Face.SIZE

它既不像使用抽象工厂那么专业,也不像一遍又一遍地使用相同的功能那么愚蠢(而且危险!)。您仍然可能会犯这样的错误:使用

Face[] faces = create(in, Faces.class, Edge.LUMP_INDEX, Edge.SIZE)

但这是一个很容易指出的错误。

You can always use the simpler form:

<T> T[] create(RandomAccessFile in, Class<T> clazz, int lumpIndex, int size)
{
    int numPlanes = doSomeCalculations(lumpIndex, size);
    T[] faces = new T[numPlanes];

    blahblah;

    return faces;
}

And use it like

Face[] faces = create(in, Faces.class, Face.LUMP_INDEX, Face.SIZE

It is neither as professional as using an abstract factory, nor as silly (and dangerous!) as having the same function over and over again. You can still make the mistake of calling it using

Face[] faces = create(in, Faces.class, Edge.LUMP_INDEX, Edge.SIZE)

But this is a mistake that it is easy to point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文