简体   繁体   English

Java如何转换为泛型类型

[英]Java how to cast to a generic type

This is my class structure 这是我的课程结构

public abstract class SuperClass

public class A extends SuperClass

public class B extends SuperClass

Now I have a frame which is going to display a list of A elements or a list of B elements, in order not to duplicate code I want to make it the most generic as possible. 现在,我有一个框架,它将显示一个A元素列表或一个B元素列表,以便不重复代码,我想使其尽可能通用。 My idea is to save the class of the elements of the frame in a field: 我的想法是将框架元素的类保存在字段中:

public class MyFrame extends Frame {
    private final Class type; // type of the elements i'm going to display
    private final DefaultListModel list; // elements to show in a SelectionList

    public <T extends SuperClass> MyFrame(final List<T> elementsToDisplay, Class<T> type) {
         this.list = createListModel(elementsToDisplay);
         this.type = type;
    }

Now it's possible to call smth like 现在可以像

List<A> list = new LinkedList<>(...);
MyFrame frame = new MyFrame(list, A.class);

My problem is how to use the field type , for example when one item is selected in the frame I want to cast it to type class, but I don't know how. 我的问题是如何使用字段类型 ,例如,当在框架中选择一项时,我想将其强制转换为类型类,但我不知道如何使用。 The following lines are pseudo-code 以下几行是伪代码

<T extend SuperClass> foo(T item) { ... }

// ----

Object selectedItem = list.get(selectionList.getSelectedIndex());
[type.class] castedItem = type.cast(selectedItem);
foo(castedItem);  

How can I cast from Object to the class that is holding type ? 如何从Object转换为持有类型的类

If your goal is really to minimize code duplication by "making it the most generic as possible" (sic), why not do 如果您的目标确实是通过“使代码尽可能最通用”来最大程度地减少代码重复,那么为什么不这样做呢?

class MyFrame<T extends SuperClass> {
    private final DefaultListModel list; 

    public MyFrame(final List<T> elementsToDisplay) {
        this.list = createListModel(elementsToDisplay);
    }
}

I would caution you, though, that it's easy to go overboard with your use of generics (especially right after you learn to use them :), and this may well be one of those cases. 不过,我要提醒您,使用泛型很容易过头(尤其是在学习使用泛型之后:),这很可能是其中一种情况。 Also: As a general rule of thumb, any time you find yourself storing the type of a generic class as a field of that class, you should see that as a red flag that you probably need to reexamine your design. 另外:作为一般经验法则,只要您发现自己将通用类的类型存储为该类的字段,就应该将其视为可能需要重新检查设计的危险信号。

if i proper understand your question the main reason to use generics is to avoid casting i suppose so at your method implementation where you know what type of object you need , you wont use the <T extends SuperClass> but <A> so that certain class will know its generic type to use. 如果我正确理解了您的问题,那么使用泛型的主要原因是避免强制转换,因此在您知道需要哪种对象的方法实现中,您不会使用<T extends SuperClass>而是使用<A>这样的特定类会知道要使用的通用类型。 if u want to keep the method with <T extends SuperClass> then you have to check the instance of class inside the method and decide which type you ll cast. 如果您想使用<T extends SuperClass>保留该方法,则必须检查该方法内部的class实例,并确定要<T extends SuperClass>类型。 fe FE

if( object instanceof A)
 ((A)object).somethinkA();

else if(object instanceof B)
 ((B)object).somethinkB();

else
.....

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

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