Java:创建一个类型为类型参数的对象

时间:2022-09-24 20:48:41

I want to write the equivalent Java code of a C# code.

我想编写C#代码的等效Java代码。

My C# code is as follows:

我的C#代码如下:

public abstract class A<T> where T : A<T>, new()
{
    public static void Process()
    {
        Process(new T());
    }

    public static void Process(T t)
    {
        // Do Something...
    }
}

public class B : A<B>
{
}

public class C : A<C>
{
}

Java equivalent of my code looks like this.

我的代码的Java等价物就像这样。

public abstract class A<T extends A<T>>
{
    public static <T extends A<T>> void process()
    {
        process(new T()); // Error: Cannot instantiate the type T 
    }

    public static <T extends A<T>> void process(T t)
    {
        // Do Something...
    }

    public class B extends A<B>
    {
    }

    public class C extends A<C>
    {
    }
}

Here the "new()" syntax in class declaration forces derived classes to write a default constructer which makes possible to call "new T()" from base class. In other words when i am wrting the base class i am sure that the derived class will have a default constructer, so that i can instantiate a derived class object from base class.

这里,类声明中的“new()”语法强制派生类编写一个默认的构造函数,从而可以从基类调用“new T()”。换句话说,当我在使用基类时,我确信派生类将具有默认构造函数,以便我可以从基类实例化派生类对象。

My problem in Java is, I cannot instantiate a derived class object from super class. I get "Cannot instantiate the type T" error for "new T()" call. Is there any C# similar way in Java or should I use something like prototype pattern and cloning?

我在Java中的问题是,我无法从超类中实例化派生类对象。我为“new T()”调用得到“无法实例化类型T”错误。在Java中是否有类似的C#方式,或者我应该使用原型模式和克隆之类的东西?

6 个解决方案

#1


5  

Java doesn't support reified generics, so there is no equivalent to "new T();". The way I work around this is to use reflection against a type token. The type token indicates what the generic type is.

Java不支持reified泛型,因此没有相当于“new T();”的东西。我解决这个问题的方法是对类型令牌使用反射。类型标记表示泛型类型。

public abstract class A<T> {
  private Class<T> typeToken;
  // constructor
  public A() {
        typeToken = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  }
}

Then use reflection to instantiate the class. It's ugly, but it gets the job done.

然后使用反射来实例化类。这很难看,但它完成了工作。

#2


2  

You can find some explanation of the difference between generics in C# and Java from this li nk - comparing java and C# generics.

你可以从这篇文章中找到C#和Java中泛型之间差异的一些解释 - 比较java和C#泛型。

Java generics are a completely compile-time construct. You cannot do anything with generic type parameters that rely in any way on runtime information. This includes:

Java泛型是一个完全编译时的结构。对于以任何方式依赖运行时信息的泛型类型参数,您无法执行任何操作。这包括:

  • Creating instances of generic type parameters.
  • 创建泛型类型参数的实例。

  • Creating arrays of generic type parameters.
  • 创建泛型类型参数的数组。

  • Quering the runtime class of a generic type parameter.
  • 查询泛型类型参数的运行时类。

  • Using instanceof with generic type parameters.
  • 使用带有泛型类型参数的instanceof。

You can bypass this restriction with java.lang.reflect namepsace. For example see this * question: Genercs and Class.forName()

您可以使用java.lang.reflect namepsace绕过此限制。例如,请参阅此*问题:Genercs和Class.forName()

#3


0  

Also, beware of this if you are using generics.

另外,如果您使用泛型,请注意这一点。

T[] someArray = new T[];

This is one reason to prefer ArrayList to arrays. The reason for the problem lies with reifiability and type erasure.

这是将ArrayList更喜欢数组的一个原因。问题的原因在于可重复性和类型擦除。

#4


0  

Just use the bog standard Abstract Factory pattern. You then get the additional benefits that you are not tying down to a specific type, the implementation type need not have a specific constructor, the instance can have some parameterisation, instances can be cached, etc., etc.

只需使用沼泽标准抽象工厂模式。然后,您将获得额外的好处,而不是特定类型,实现类型不需要特定的构造函数,实例可以有一些参数化,实例可以缓存等等。

For the love of god, don't use reflection.

为了上帝的爱,不要使用反射。

#5


0  

In addition to the other comments, I would suggest not using generics. They are not needed--they get stripped out at compile time anyway--and if you do not know them well you will try to make them do things they cannot.

除了其他评论,我建议不要使用泛型。它们不是必需的 - 无论如何它们都会在编译时被剥离 - 如果你不熟悉它们,你会试着让它们做他们做不到的事情。

Once you have your class working properly, then add them back in. Your IDE will, at that point, give you a lot of useful and intelligible advice, and the generics will warn you when you use objects of the wrong class.

一旦你的课程正常工作,然后重新添加。你的IDE将在那时为你提供许多有用和可理解的建议,当你使用错误类的对象时,泛型会警告你。

It does look possible to me that this class will not need generics at all when finished. (I don't know what else this class may do, and I do not understand the use of the static methods--they will never have access to an individual instance's type information.)

我觉得这个类在完成后根本不需要泛型。 (我不知道这个类可以做什么,我不明白静态方法的使用 - 他们永远不会访问单个实例的类型信息。)

#6


0  

Actually this is not a problem in Java. The idiom is passing the class

实际上这不是Java中的问题。这个成语正在通过课堂

    public static <T extends A<T>> T process(Class<T> clazz) 
    {
        T o = clazz.newInstance();
        process( o ); 
        return o;
    }

    X x = process(X.class); // not too verbose

I added a return value to illustrate the general case.

我添加了一个返回值来说明一般情况。

#1


5  

Java doesn't support reified generics, so there is no equivalent to "new T();". The way I work around this is to use reflection against a type token. The type token indicates what the generic type is.

Java不支持reified泛型,因此没有相当于“new T();”的东西。我解决这个问题的方法是对类型令牌使用反射。类型标记表示泛型类型。

public abstract class A<T> {
  private Class<T> typeToken;
  // constructor
  public A() {
        typeToken = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
  }
}

Then use reflection to instantiate the class. It's ugly, but it gets the job done.

然后使用反射来实例化类。这很难看,但它完成了工作。

#2


2  

You can find some explanation of the difference between generics in C# and Java from this li nk - comparing java and C# generics.

你可以从这篇文章中找到C#和Java中泛型之间差异的一些解释 - 比较java和C#泛型。

Java generics are a completely compile-time construct. You cannot do anything with generic type parameters that rely in any way on runtime information. This includes:

Java泛型是一个完全编译时的结构。对于以任何方式依赖运行时信息的泛型类型参数,您无法执行任何操作。这包括:

  • Creating instances of generic type parameters.
  • 创建泛型类型参数的实例。

  • Creating arrays of generic type parameters.
  • 创建泛型类型参数的数组。

  • Quering the runtime class of a generic type parameter.
  • 查询泛型类型参数的运行时类。

  • Using instanceof with generic type parameters.
  • 使用带有泛型类型参数的instanceof。

You can bypass this restriction with java.lang.reflect namepsace. For example see this * question: Genercs and Class.forName()

您可以使用java.lang.reflect namepsace绕过此限制。例如,请参阅此*问题:Genercs和Class.forName()

#3


0  

Also, beware of this if you are using generics.

另外,如果您使用泛型,请注意这一点。

T[] someArray = new T[];

This is one reason to prefer ArrayList to arrays. The reason for the problem lies with reifiability and type erasure.

这是将ArrayList更喜欢数组的一个原因。问题的原因在于可重复性和类型擦除。

#4


0  

Just use the bog standard Abstract Factory pattern. You then get the additional benefits that you are not tying down to a specific type, the implementation type need not have a specific constructor, the instance can have some parameterisation, instances can be cached, etc., etc.

只需使用沼泽标准抽象工厂模式。然后,您将获得额外的好处,而不是特定类型,实现类型不需要特定的构造函数,实例可以有一些参数化,实例可以缓存等等。

For the love of god, don't use reflection.

为了上帝的爱,不要使用反射。

#5


0  

In addition to the other comments, I would suggest not using generics. They are not needed--they get stripped out at compile time anyway--and if you do not know them well you will try to make them do things they cannot.

除了其他评论,我建议不要使用泛型。它们不是必需的 - 无论如何它们都会在编译时被剥离 - 如果你不熟悉它们,你会试着让它们做他们做不到的事情。

Once you have your class working properly, then add them back in. Your IDE will, at that point, give you a lot of useful and intelligible advice, and the generics will warn you when you use objects of the wrong class.

一旦你的课程正常工作,然后重新添加。你的IDE将在那时为你提供许多有用和可理解的建议,当你使用错误类的对象时,泛型会警告你。

It does look possible to me that this class will not need generics at all when finished. (I don't know what else this class may do, and I do not understand the use of the static methods--they will never have access to an individual instance's type information.)

我觉得这个类在完成后根本不需要泛型。 (我不知道这个类可以做什么,我不明白静态方法的使用 - 他们永远不会访问单个实例的类型信息。)

#6


0  

Actually this is not a problem in Java. The idiom is passing the class

实际上这不是Java中的问题。这个成语正在通过课堂

    public static <T extends A<T>> T process(Class<T> clazz) 
    {
        T o = clazz.newInstance();
        process( o ); 
        return o;
    }

    X x = process(X.class); // not too verbose

I added a return value to illustrate the general case.

我添加了一个返回值来说明一般情况。