gpt4 book ai didi

Java 泛型 : Require generic to be subclass of a certain type

转载 作者:搜寻专家 更新时间:2023-10-31 19:40:26 27 4
gpt4 key购买 nike

我有一个抽象泛型类:

public abstract class AbstractMessageHandler<T extends AbstractMessageHandler>
{
public abstract List<String> getTypesOfMessages();
public abstract void handleMessage(String message, CometClient client);

public T setResponseValues(AbstractMessage request, T response )
{
response.setCompanyId(request.getCompanyId());
response.setMessageGroup(request.getMessageGroup());
response.setUserId(request.getUserId());
response.setTimeStamp(AbstractMessage.getCurrentTimeStamp());

return response;
}
}

我需要通用子类成为此类的子类。换句话说,泛型必须是 AbstractMessageHandler 的子类。然而,这给了我编译问题。谁能告诉我我做错了什么?

谢谢

最佳答案

你需要按照枚举类的例子:

public abstract class AbstractMessageHandler<T extends AbstractMessageHandler<T>>

关于Java 泛型 : Require generic to be subclass of a certain type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12628216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com