泛型接口
2016-07-09 13:00
369 查看
在类上自定义泛型,T不能是基本数据类型,如int,需用Integer。
泛型接口:
interface 接口名<声明自定义泛型> {
...
}
interface Dao<T> {
public void add(T t);
}
①,确定类型。
public class Demo implements Dao<String< {
public void add(String t) {
}
}
②,未确定类型。
public class Demo<T> implments Dao<T> {
public void main(String[] args) {
Demo<String> d = new Demo<String>();
}
public void add(T t) {
}
}
泛型接口:
interface 接口名<声明自定义泛型> {
...
}
interface Dao<T> {
public void add(T t);
}
①,确定类型。
public class Demo implements Dao<String< {
public void add(String t) {
}
}
②,未确定类型。
public class Demo<T> implments Dao<T> {
public void main(String[] args) {
Demo<String> d = new Demo<String>();
}
public void add(T t) {
}
}
package 泛型接口; interface test<T> { public void add(T t); } //方式一。未确定类型,延迟接口自定义类型 /*public class Interfacetest<T> implements test<T>{ public static void main(String[] args) { Interfacetest<String> tf = new Interfacetest<String>(); tf.add("ok"); } @Override public void add(T t) { System.out.println("InterfaceTest....." + t); } }*/ //方式二。确定 接口自定义类型 public class Interfacetest implements test<String> { public static void main(String[] args) { Interfacetest ift = new Interfacetest(); ift.add("ok2"); } public void add(String t) { System.out.println("add.."+t); } }
package 泛型接口; class Tool<T> { //1.方式一 public void print(T[] t) { System.out.println("print..."+ t[1]); } //2.方式二。在类上自定义泛型不能作用于静态方法。如果静态方法需要自定义泛型,则要在方法上声明使用 public static <T>void method(T[] t) { System.out.println("method.."+t[2]); } } public class MyArray { public static void main(String[] args) { Integer[] arr = {1,2,3}; //1.T通过创建对象传递 Tool<Integer> tool = new Tool<Integer>(); tool.print(arr); //2.T通过参数传递 } }
相关文章推荐
- UITableViewCell的backGroundView和selectedBackgroundView在cell中的层次
- linux信号--阻塞信号
- WCF安全机制之自定义账号密码
- Javascript继承机制
- 字符串替换
- iOS-打包成ipa的4种方法
- 2016/7/9重学了一波递归和递推
- 今天开始正式入住CSDN
- 清除缓存功能的添加
- [Material Design开发系列1]AppCompat V21:将MD兼容到5.0之前的设备
- 编写两个进程a和b,利用共享内存技术,a向共享内存写字符串,b将从共享内存中读到的字符串在屏幕上打印出来。
- Linux的SOCKET编程详解
- 2.一个EJB的小Demo
- Vim里调试错误小结(一):程序中有游离的 ‘\200’
- 编写两个不同的可执行程序,一个打开文件,一个读文件
- VS2015下Log4Cplus编译及其使用
- 对于javascript四舍五入保留多少位小数的问题
- Alamofire请求网络
- iOS-最全的App上架教程
- Subtree