Java 在泛型中使用继承有问题的声明

Java 在泛型中使用继承有问题的声明,java,generics,inheritance,interface,Java,Generics,Inheritance,Interface,我有 LoginCommandExecutor实现CommandExecutor LoginCommand实现该命令 此行引发编译错误的原因: LoginCommandExecutor implements CommandExecutor<LoginCommand> LoginCommand implements Command CommandExecutor a=新的LoginCommandExecutor(commander,null); 但它适用于以下两种情况: Comm

我有

LoginCommandExecutor实现CommandExecutor
LoginCommand实现该命令
此行引发编译错误的原因:

LoginCommandExecutor implements CommandExecutor<LoginCommand>
LoginCommand implements Command
CommandExecutor a=新的LoginCommandExecutor(commander,null);
但它适用于以下两种情况:

CommandExecutor<Command> a = new LoginCommandExecutor(commander, null);
命令执行器
使用原始类型。绝对不应该使用它

CommandExecutor b = new LoginCommandExecutor(commander, null);
命令执行器
使用原始类型。绝对不应该使用它

CommandExecutor b = new LoginCommandExecutor(commander, null);

谢谢。但这不是与OOP继承相反吗
CommandExecutor a
公开了基本类方法,这些方法是
LoginCommand
无论如何都要实现的。不反之亦然:这并不意味着
LoginCommand
可以强制转换为继承基类的任何类(又称
Command
),LoginCommandExecutor只能处理LoginCommand。因此,您确实不希望能够向它提交其他类型的命令。就像您不想将整数存储到
列表中一样,我只是不明白声明的区别:
CommandExecutor
CommandExecutor
表示“接受任何命令实例的命令执行器”
CommandExecutor
表示“接受任何命令实例的命令执行器”
,但这仍然可以<代码>登录命令执行命令
。我希望所有这些都能在地图上填上许多命令。但这不是与OOP继承相反吗
CommandExecutor a
公开了基本类方法,这些方法是
LoginCommand
无论如何都要实现的。不反之亦然:这并不意味着
LoginCommand
可以强制转换为继承基类的任何类(又称
Command
),LoginCommandExecutor只能处理LoginCommand。因此,您确实不希望能够向它提交其他类型的命令。就像您不想将整数存储到
列表中一样,我只是不明白声明的区别:
CommandExecutor
CommandExecutor
表示“接受任何命令实例的命令执行器”
CommandExecutor
表示“接受任何命令实例的命令执行器”
,但这仍然可以<代码>登录命令执行命令
。我希望所有这些都能用许多命令填充地图
CommandExecutor<? extends Command> a = new LoginCommandExecutor(commander, null);
CommandExecutor<Command> a = new LoginCommandExecutor(commander, null);
CommandExecutor<Command> a = new LoginCommandExecutor(commander, null);
a.submit(new WhateverCommand());