黑马程序员--泛型
来源:互联网 发布:csol for mac 编辑:程序博客网 时间:2024/06/11 05:42
------- android培训、java培训、期待与您交流! ----------
但是对于集合,他可以接受任何引用数据类型的对象,泛型之前的容器需要接受参数的大都是Object类型的,所以从集合框架中取出的数据还是Object类型的,还需要对其进行向下转型,还有因为他可以存储任何类型,你既可以存入Person对象,也可以存入Car类型,什么都可以往里面装,而且编译器还不能检测到语法错误。等到运行是才出现问题。程序的安全性不好。所以泛型来了。int[ ] num = {1,5,6,7};
String[ ] str = {"abc","fdg","uio"};
class GenericDemo { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>();
al.add("abc01"); al.add("abc0991"); al.add("abc014"); for(Iterator<String> it = al.iterator();it.hasNext();)
{ String s = it.next(); System.out.println(s+":"+s.length()); } }}
/*class Tool{ private Worker w; public void setWorker(Worker w) { this.w = w; } public Worker getWorker() { return w; }}*/class Worker{ //do something}class Student{ //do something}//泛型前做法。class Tool{ private Object obj; public void setObject(Object obj) { this.obj = obj; } public Object getObject() { return obj; }}//泛型类。class Utils<QQ>{ private QQ q; public void setObject(QQ q) { this.q = q; } public QQ getObject() { return q; }}
class GenericDemo3{ public static void main(String[] args) {
Utils<Worker> u = new Utils<Worker>(); u.setObject(new Student()); Worker w = u.getObject();; /* Tool t = new Tool(); t.setObject(new Student()); Worker w = (Worker)t.getObject(); */ }}
泛型方法与泛型类比较示例代码:
说明:泛型类和泛型方法没有绝对的界限,而且可以混合使用,如果要操作某一特定类型T的数据,
就可以定义为泛型类,所有方法也可以定义为T类型,就像集合中的那样。
而泛型方法更具灵活性,可以操作所传入的任意类型。
class Demo<T>{
//操作的类型为类上定义的<T> public void show(T t) { System.out.println("show:"+t); }
//与类上的泛型无关,可以接受任何类型 public <Q> void print(Q q) { System.out.println("print:"+q); }
//静态方法不能用类上定义的泛型,只能定义在方法上 public static <W> void method(W t) { System.out.println("method:"+t); }}class GenericDemo4 { public static void main(String[] args) { Demo <String> d = new Demo<String>(); d.show("haha"); //d.show(4); d.print(5); d.print("hehe"); Demo.method("hahahahha"); /* Demo d = new Demo(); d.show("haha"); d.show(new Integer(4)); d.print("heihei"); */ /*
//只有对象建立了才能确定数据类型,所以静态方法不能用类上定义的泛型
Demo<Integer> d = new Demo<Integer>(); d.show(new Integer(4)); d.print("hah"); Demo<String> d1 = new Demo<String>(); d1.print("haha"); d1.show(5); */ }}
/泛型定义在接口上。 interface Inter<T>{ void show(T t); } /*将接口实现定义为String,只能操作String类型数据 class InterImpl implements Inter<String>{ public void show(String t){ System.out.println("show :"+t); } } */ //将接口实现类定义为不确定的类型,操作传入的类型 class InterImpl<T> implements Inter<T>{ public void show(T t){ System.out.println("show :"+t); } } class GenericDemo5 { public static void main(String[] args) { InterImpl<Integer> i = new InterImpl<Integer>(); i.show(4); //InterImpl i = new InterImpl(); //i.show("haha"); } }
注意:
在使用下限的时候,一般在传入的为父类,因为这样,只要是子类就可以接收,提高了扩展性,但是只能用父类的方法。
示例二:import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;class Person4 { private String name; Person4(String name) { this.name = name; } public String getName() { return name; }}class Student4 extends Person4 { private String name; Student4(String name) { super(name); } // 这里定义之后打印的是null,因为覆盖了父类的getName方法,但是初始化是到父类初始化, // 所以使用的不是同一个name,本类中的name并没有赋值 public String getName() { return name; }}class GenericDemo6 { public static void main(String[] args) { /* ArrayList<String> al = new ArrayList<String>(); al.add("abc1"); al.add("abc2"); al.add("abc3"); printColl(al); ArrayList<Integer> al1 = new ArrayList<Integer>(); al1.add(4); al1.add(7); al1.add(1); printColl(al1); */ ArrayList<Person4> al = new ArrayList<Person4>(); al.add(new Person4("abc1")); al.add(new Person4("abc2")); al.add(new Person4("abc3")); printColl(al); ArrayList<Student4> al1 = new ArrayList<Student4>(); al1.add(new Student4("abc--1")); al1.add(new Student4("abc--2")); al1.add(new Student4("abc--3")); printColl(al1); // ArrayList<? extends Person> al = new // ArrayList<Student>();error } public static void printColl(Collection<? extends Person4> al)// 接收Person及Person的子类型,多态 { Iterator<? extends Person4> it = al.iterator(); while (it.hasNext()) { System.out.println(it.next().getName()); } } /* 如果明确定义为<T>类型,可以接收并操作T类型,而<?>因为没确定类型, 所以不可以使用类型的特有方法 public static void printColl(ArrayList<?> al){ //ArrayList al = new ArrayList<Integer>();error Iterator<?> it = al.iterator(); while(it.hasNext()) { System.out.println(it.next().toString()); } } */}
import java.util.ArrayList;import java.util.Collection;import java.util.Iterator;class Person4 { private String name; Person4(String name) { this.name = name; } public String getName() { return name; }}class Student4 extends Person4 { private String name; Student4(String name) { super(name); } // 这里定义之后打印的是null,因为覆盖了父类的getName方法,但是初始化是到父类初始化, // 所以使用的不是同一个name,本类中的name并没有赋值 public String getName() { return name; }}class GenericDemo6 { public static void main(String[] args) { /* ArrayList<String> al = new ArrayList<String>(); al.add("abc1"); al.add("abc2"); al.add("abc3"); printColl(al); ArrayList<Integer> al1 = new ArrayList<Integer>(); al1.add(4); al1.add(7); al1.add(1); printColl(al1); */ ArrayList<Person4> al = new ArrayList<Person4>(); al.add(new Person4("abc1")); al.add(new Person4("abc2")); al.add(new Person4("abc3")); printColl(al); ArrayList<Student4> al1 = new ArrayList<Student4>(); al1.add(new Student4("abc--1")); al1.add(new Student4("abc--2")); al1.add(new Student4("abc--3")); printColl(al1); // ArrayList<? extends Person> al = new // ArrayList<Student>();error } public static void printColl(Collection<? extends Person4> al)// 接收Person及Person的子类型,多态 { Iterator<? extends Person4> it = al.iterator(); while (it.hasNext()) { System.out.println(it.next().getName()); } } /* 如果明确定义为<T>类型,可以接收并操作T类型,而<?>因为没确定类型, 所以不可以使用类型的特有方法 public static void printColl(ArrayList<?> al){ //ArrayList al = new ArrayList<Integer>();error Iterator<?> it = al.iterator(); while(it.hasNext()) { System.out.println(it.next().toString()); } } */}
- 《黑马程序员》泛型
- 黑马程序员—泛型
- <黑马程序员>泛型
- 黑马程序员-Java泛型
- 黑马程序员_泛型
- 黑马程序员 泛型
- 黑马程序员--泛型
- 黑马程序员-java-泛型
- 黑马程序员_泛型
- 黑马程序员_Java泛型
- 黑马程序员_泛型
- 黑马程序员_泛型
- 黑马程序员-泛型Generic
- 黑马程序员:泛型
- 黑马程序员_泛型
- 黑马程序员--泛型
- 黑马程序员---高新技术 泛型
- 黑马程序员:泛型
- 关于 STLport,Boost和Qt的思考
- 【leetcode】面试leetcode代码
- 白话机器学习算法(二)KNN
- Valera and X
- 学习OpenCV(三)用filter2D实现矩阵掩码操作
- 黑马程序员--泛型
- 浏览器探究——APP层UI布局
- Android视觉规范-间距规范与文字规范单位换算(dip、sp与px)
- nginx配置
- LeetCode Partition List
- 简单的时间获取C/S程序
- 剔除冗余空格字符
- XMPP文件传输(XEP-0096协议说明)
- android的低内存管理器