简体   繁体   English

Java泛型和原始类型

[英]Java generics and raw type

When I write the method this way. 当我以这种方式编写方法时。 I get this warning: 我收到此警告:

BaseEvent is a raw type. BaseEvent是原始类型。 References to generic type BaseEvent should be parameterized 泛型类型BaseEvent的引用应参数化

@Override
public <T extends BaseEvent> void actionPerformed(T event) { ... }

The code still runs fine, although the warning sign is annoying. 尽管警告信号很烦人,但是代码仍然可以正常运行。 When I write the code this way the warning goes away. 当我以这种方式编写代码时,警告消失了。

@Override
public <T> void actionPerformed(BaseEvent<T> event) { ... }

With the previous message, It doesn't guarantee that is a subClass of BaseEvent. 对于上一条消息,它不能保证它是BaseEvent的子类。 So I changed it again: 所以我再次更改了它:

@Override
public <T extends EventObject> void actionPerformed(BaseEvent<T> event) { ... }


@Override
public <T extends BaseEvent<T>> void actionPerformed(BaseEvent<T> event) { ... }

BaseEvent class is a class I made that extends EventOBject BaseEvent类是我制作的用于扩展EventOBject的类

public abstract class BaseEvent<T> extends EventObject
{
    private String eventType;

    // Constructor
    public BaseEvent(Object source, String type) 
    {
        super(source);
        eventType = type;
    }


    public String getEventType()    { return eventType; }   
}

All the methods seem to work fine. 所有方法似乎都可以正常工作。 But I was wondering which is the better solution. 但是我想知道哪种方法更好。

Where do you use T in BaseEvent definition? 您在BaseEvent定义中在哪里使用T Define it in the following way 通过以下方式定义

public abstract class BaseEvent extends EventObject

then you won't get a warning with 那么您将不会收到警告

@Override
public void actionPerformed(BaseEvent event) { ... }

UPDATE UPDATE

Suppose your BaseEvent really required to be parametrized. 假设您的BaseEvent确实需要参数化。 Then write following 然后写以下

@Override
public <T> void actionPerformed(BaseEvent<T> event) { ... }

This will give you a parametrized method. 这将为您提供参数化方法。

UPDATE 1 更新1

It doesn't guarantee that is a subClass of BaseEvent.

It does. 是的 <T> is a parameter for method template. <T>是方法模板的参数。 This parameter goes to BaseEvent<T> which is subclass of EventObject by definition. 此参数去BaseEvent<T>其是亚类EventObject通过定义。

UPDATE 2 更新2

Do not use generics at the beginning of your learning. 在学习之初,请勿使用泛型。 Generics are just for additional self testing. 泛型仅用于其他自我测试。 Use raw types. 使用原始类型。 Then when you start to feel them, you will parametrize them correctly. 然后,当您开始感觉到它们时,就可以正确地参数化它们。

The best solution is the one that avoids the warnings and guarantees your type safety at compile time. 最好的解决方案是避免警告并在编译时确保类型安全的解决方案。

If the type of T in BaseEvent doesn't matter, couldn't you just use your first one and parameterize BaseEvent? 如果BaseEvent中T的类型无关紧要,您是否不能只使用第一个参数化BaseEvent? Do something like: 做类似的事情:

@Override
public <T extends BaseEvent<?>> void actionPerformed(T event) { ... }

Alternatively, it looks like your BaseEvent class does not actually use T for anything - why is it there? 另外,看起来您的BaseEvent类实际上没有使用T进行任何操作-为什么在其中呢?

The type parameter T is never used in the class definition. 类型参数T从未在类定义中使用。 You might be able to remove the type parameter from BaseEvent : 您也许可以从BaseEvent删除type参数:

public abstract class BaseEvent extends EventObject { ... }

and just define your method without a type parameter: 并只定义没有类型参数的方法:

@Override
public void actionPerformed(BaseEvent event) { ... }

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

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