ZhenyuGo

导航

Java泛型学习笔记 - (五)泛型接口

所谓泛型接口, 类似于泛型类, 就是将泛型定义在接口上, 其格式如下:
  public interface 接口名<类型参数>
如:

1 interface Inter<T> {
2     public void show(T t);
3 }

其实现方式有两种:
1.在实现时指定泛型:

1 class A implements Inter<String> {
2 
3     @Override
4     public void show(String t) {
5         System.out.println(t);
6     }
7     
8 }

2. 在实例化时再指定泛型:

1 class B<T> implements Inter<T> {
2 
3     @Override
4     public void show(T t) {
5         System.out.println(t);
6     }
7     
8 }

调用:

1 A a = new A();
2 a.show("Hi!");
3 B<Boolean> b = new B<>();
4 b.show(true);

 

posted on 2016-05-28 01:16  ZhenyuGo  阅读(625)  评论(0编辑  收藏  举报