参数的java泛型类型与抽象类

参数的java泛型类型与抽象类,java,generics,parameters,abstract,generic-type-argument,Java,Generics,Parameters,Abstract,Generic Type Argument,我正在学习Java泛型类型 我有一个抽象类AbstractInputdata public abstract class AbstractInputData { .... } 一些扩展AbstractInputData的类 public class Email extends AbstractInputData{ ... } public class Mobile extends AbstractInputData{ ... } ...... A B 公共类处理器b{ 公共静态布尔isCus

我正在学习Java泛型类型

我有一个抽象类AbstractInputdata

public abstract class AbstractInputData {
....
}
一些扩展AbstractInputData的类

public class Email extends AbstractInputData{
...
}
public class Mobile extends AbstractInputData{
...
}
......
A

B

公共类处理器b{
公共静态布尔isCustomData(T){
...
}
}

A和B之间有什么区别吗?

唯一的区别是,第二个方法通过反射显示为泛型类型的方法。它的行为将是相同的,除非在这种奇怪的情况下

processorB.<MyType>isCustomData(t); // won't compile unless t is a MyType
processorB.isCustomData(t);//除非t是MyType,否则不会编译

您必须告诉它您希望它匹配的类型,这在IMHO中不是很有用。

因为您的方法只生成布尔值,所以没有区别。但如果要返回输入,可以使用B保留泛型类型:

public class ProcessorB {
  public static <T extends AbstractInputData> boolean isCustomData(T t) {
    ...
  }
  public static <T extends AbstractInputData> T copyCustomData(T t) {
    ...
  }
}
公共类处理器b{
公共静态布尔isCustomData(T){
...
}
公共静态T copyCustomData(T){
...
}
}

ProcessorA只能返回AbstractInputData类型的对象,而processorB根据参数类型返回电子邮件或手机。

@SayemAhmed泛型已经存在了9年左右,仍然令人困惑。想象一下lambda将多么混乱:P,这可能会帮助您理解generic+1。谢谢在我测试了一些案例之后,我终于得到了它@汤姆沃克
processorB.<MyType>isCustomData(t); // won't compile unless t is a MyType
public class ProcessorB {
  public static <T extends AbstractInputData> boolean isCustomData(T t) {
    ...
  }
  public static <T extends AbstractInputData> T copyCustomData(T t) {
    ...
  }
}