English 中文(简体)
采用方法上的通用等级
原标题:Using the Generic Class from the Method Result of a class

我想利用在 Java采用通用方法所掌握的类别信息,以可能树立一个榜样。 但是,我看不出如何在没有实际证据的情况下提及这一类。 是否可能?

public class Fancy {

static public <TypeToFind> TypeToFind createInSomeCase() {
  // any type of logic with TypeToFind "class" generic will do, I just want to reference it.

  // the below code is invalid, I could also declare a variable, but can t always create an instance to get the class
  if (TypeToFind.getClass().equals(Something.class)) {
    return TypeToFind.getInstance();
  }
}


}

......后来我可以这样做:

TheResultIsTheParameter t = Fancy.createInSomeCase();

......

TheResultIsAParameter t = Fancy.createInSomeCase(TheResultIsAParameter.class);

......

TheResultIsAParameter t = Fancy.createInSomeCase(t);

我这样说太复杂了?

最佳回答

您可以这样做,因为普通人在经营时间丢失(见 rel=“noreferer”> 字幕:)。 您必须通过<代码>Class<?>参数

问题回答

确实,你需要一些思索,即 Java的不舒服是逻辑的,只是一个syntactic food/strong>来思考。

List<MyClass> list;
(...)
MyClass my = list.get(0);

将汇编成册。

MyClass my = (MyClass) list.get(0);

这正是你在密码中看到的。

更有甚者,通过思考或投出无类名单,你可将任何物体列入<条码><>>> > 名单/代码>,并在两份代码上填写:<条码>。 类别CastException。

因此,一般资料只存在于汇编一级。 一个没有新内容的大型特征只是缩短了 多数案件的代码。

As long as you do not try and statically (at compile time) reference any particular class, nothing prevents you from doing something like this:

public class GenericsTest {
   @Test
   public void testMe() {
      GenericsTest test = new GenericsTest();
      System.out.println(test.get("Hello").getClass());
   }

   public GenericsTest() {
      super();
   }

   public <T extends Object> T get(T entity) {
      return newInstanceForClass((Class<T>)entity.getClass());
   }

   public <T extends Object> T newInstanceForClass(Class<T> clazz) {
      try {
         return clazz.newInstance();
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }
}

但是,正如你可以看到的那样,你需要把你试图当场的那类人当作一个目标,这或许不会是想你来的。 在这种情形下,唯一的其他选择是,以其他海报雄辩地表明的理由,通过按通用类型划分的等级参数。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...