简体   繁体   English

在Java中使用通用类型

[英]using generic type in java

trying to learn java generic feature but while using it i am getting a warning and unable to understand how i can solve it,though the program in itself is running fine. 尝试学习Java通用功能,但是使用该功能时,我收到警告,并且无法理解如何解决,尽管程序本身运行良好。

i have created a class with following signature 我创建了一个具有以下签名的类

public class MyClass<T> {

    public T demo(String string,Class<T> type){
        // some work 
    }

}

now in my other class i am declaring instance of this class as follow 现在在我的另一个班级中,我声明该班级的实例如下

private MyClass myClass;

and than i am trying to call this method from some places like 而且比我试图从某些地方调用这种方法

(ClassB)myClass.demo("hello",ClassB.class);
(ClassC)myClass.demo("hello",ClassC.class);

The program is working fine but i am finding this warning in eclipse. 该程序运行正常,但我在日食中发现此警告。

Type safety: The method demo(String, Class) belongs to the raw type MyClass. References to generic 
 type MyClass<T> should be parameterized

can any one help me to understand how i can handle this warning? 有谁能帮助我了解如何处理此警告?

update 更新

i am using spring to inject myClass instance so can't use new operator here 我正在使用spring注入myClass实例,所以不能在这里使用new运算符

Try this instead 试试这个

public class MyClass {

public <T> T demo(String json,Class<T> type){
    // some work  
}

}

You need to declare your var as: 您需要将var声明为:

private MyClass<ClassB> myClassClassB;

---- update ----更新

If you do not know the type of the generic initially, you can use this: 如果最初不知道泛型的类型,则可以使用以下方法:

private MyClass<? extends Class> myClass;

That will get rid of the warning, though I think you are not fully understanding generics. 尽管我认为您还没有完全理解泛型,但是这将消除警告。 What is it you are trying to accomplish with this? 您正在尝试完成什么? You are having spring inject your myClass variable with an implemention of MyClass, but the Generics dont much help because generics only mean anything at compile time, and Dependency Injection happens at run time... 您正在用MyClass的实现对myClass变量进行Spring注入,但是Generic并没有太大帮助,因为Generic仅在编译时具有任何意义,而Dependency Injection在运行时发生...

Because of the way you declared your class, Java expects that you give it a type parameter when declaring/initializing a class of that type. 由于声明类的方式,Java希望在声明/初始化该类型的类时为其提供一个类型参数。 So in this case you'd need to do: 因此,在这种情况下,您需要执行以下操作:

private MyClass<ClassB> myclass = new MyClass<ClassB>();

What you try to achieve by calling 您通过致电尝试实现的目标

(ClassB)myClass.demo("hello",ClassB.class);
(ClassC)myClass.demo("hello",ClassC.class);

can be achieved if you remove the <T> from your class. 如果从班级中删除<T> ,则可以实现。 So replace 所以更换

public class MyClass<T>

by 通过

public class MyClass

and changing the signature of the method to 并将方法的签名更改为

public <T> T demo(String json,Class<T> type)

I would recommend reading the Generics tutorial , but this part of the tutorial shows how and explains why 我建议阅读Generics教程 ,但是该部分介绍了操作方法并解释了原因

Basically, your class definition public class MyClass<T> tells the compiler that MyClass uses a generic type. 基本上,您的类定义public class MyClass<T>告诉编译器MyClass使用泛型类型。 But when you create an instance of it in private MyClass<SomeType> myClass; 但是,当您在private MyClass<SomeType> myClass;创建它的实例时private MyClass<SomeType> myClass; you did not provide the type to use. 您未提供要使用的类型。 Use: 采用:

private MyClass<ClassB> myClass;

or 要么

private MyClass<ClassC> myClass;

instead of: 代替:

private MyClass myClass;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM