- 对于泛型接口的子类而言,有2种实现方式
demo1 在子类中继续进行泛型定义
interface IMessage<T> {
public String echo(T t);
}
class MessageImpl<S> implements IMessage<S> {
public String echo(S t){
return "[echo]" + t;
}
}
public class HelloWorld {
public static void main(String args[]){
IMessage<String> msg = new MessageImpl<String>();
System.out.println(msg.echo("www.baidu.com"));
}
}
demo2 2.在子类实现父类接口的时候,直接定义出具体泛型类型.
interface IMessage<T> {
public String echo(T t);
}
class MessageImpl implements IMessage<String> {
public String echo(String t){
return "[echo]" + t;
}
}
public class HelloWorld {
public static void main(String args[]){
IMessage<String> msg = new MessageImpl();
System.out.println(msg.echo("www.baidu.com"));
}
}
标签:String,9.16,接口,echo,msg,IMessage,泛型,public
From: https://www.cnblogs.com/pansidong/p/17473251.html