泛型
来源:互联网 发布:天津网络推广公司排名 编辑:程序博客网 时间:2024/04/26 05:05
泛型是用于解决安全问题的,用于解决安全机制
好处
1.将运行时期出现问题ClassCastException,转移到了编译时期。
方便于程序员解决问题。让运行时期问题减少,安全。
2.避免了强制转换的麻烦。
泛型格式:通过<>来定义要操作的引用数据类型。
在使用java提供的对象时,什么时候写泛型呢?
通常在集合框架中很常见,只要见到<>就要定义泛型
其实<>就是用来接收类型的。当使用集合时,将集合中要存储的数据类型作为参数传递到<>中即可。
class GenericDemo{ public static void main(String[] args) { ArrayList<String>a1 = new ArrayList<String>(); //强制只能存储字符串类型 a1.add("abc01"); a1.add("abc02"); a1.add("abc03"); Iterator<String> it = a1.iterator(); while(it.hasNext()) { String s = it.next(); System.out.println(s+":"+s.length()); } }}
泛型类
什么时候定义泛型类?
当类中要操作的引用数据类型不确定的时候,定义泛型来完成扩展。
定义格式
* public class 类名<泛型类型1,…>
class Worker{}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>(); //只能操作Worker类型 u.setObject(new Worker()); Worker w = u.getObject(); }}
泛型方法
泛型类定义的泛型,在整个类中有效。如果被方法使用,那么泛型类的对象明确要操作的具体类型后,所有要操作的类型就已经固定了。
为了让不同方法可以操作不同类型,而且类型还不确定。那么可以将泛型定义在方法上。
定义格式
* public <泛型类型> 返回类型 方法名(泛型类型 变量名)
class Demo{ public <T> void show(T t) { System.out.println("show:"+t); } public <Q> void print(Q q) { System.out.println("print:"+q); }}class GenericDemo4{ public static void main(String[] args) { Demo d = new Demo(); d.show("haha"); d.show(new Integer(4)); d.print("heihei"); d.print(5); }}
特殊之处:
静态方法不可以访问类上定义的泛型,如果静态方法操作的引用数据类型不确定,可以将泛型定义在方法上。
class Demo<T>{ public <T> void show(T t) { System.out.println("show:"+t); } public <Q> void print(Q q) { System.out.println("print:"+q); } /* public static void method(T t) 这样错误 { System.out.println("method:"+t); } */ public static <W> void method(W w) { System.out.println("method:"+w); }}class GenericDemo4{ public static void main(String[] args) { Demo<String> d = new Demo<String>(); d.show("haha"); //d.show(4); 只能打印T 也就是String类型 d.print("heihei"); d.print(5); Demo.method("hahahhaha"); }}
泛型接口
泛型定义在接口上
看如下代码:
interface Inter<T>{ void show(T t);}calss InterImpl implements Inter<String> //传入String类型{ public void show(String t) { System.out.println("show:"+t) }}calss GenericDemo5{ public static void main(String[] args) { InterImpl i = new InterImpl(); i.show("haha"); //只能输入String类型 }}
interface Inter<T>{ void show(T t);}calss InterImpl<T> implements Inter<T> { public void show(T t) { System.out.println("show:"+t) }}calss GenericDemo5{ public static void main(String[] args) { InterImpl<Integer> i = new InterImpl<Integer>(); //这样可以由调用者决定操作类型 i.show(4); }}
泛型限定
class GenericDemo6{ public static void main(String[] args) { ArrayList<String> a1 = new ArrayList<String>(); a1.add("abc1"); a1.add("abc2"); a1.add("abc3"); ArrayList<Integer> a2 = new ArrayList<Integer>(); a2.add("4"); a2.add("5"); a2.add("6"); printColl(a1); printColl(a2); } public static void printColl(ArrayList a1) { Iterator it = a1.iterator(); while(it.hasNext()) { System.out.println(it.next()); } }}
都可以输出,但不能用类型的特有方法 , 比如length 输出长度,这是String类型的特有方法。
import java.utils.*;class GenericDemo6{ public static void main(String[] args) { ArrayList<Person> a1 = new ArrayList<String>(); a1.add(new Person("abc1")); a1.add(new Person("abc2")); a1.add(new Person("abc3")); printColl(a1); ArrayList<Student> a2 = new ArrayList<Student>(); a2.add(new Student("abc4")); a2.add(new Student("abc5")); a2.add(new Student("abc6")); //printColl(a2); //ArrayList<Person> a1 = new ArrayList<Student>(); 错误,必须保证左右相同 } public static void printColl(ArrayList<Person> a1) { Iterator<Person> it = a1.iterator(); while(it.hasNext()) { System.out.println(it.next().getName()); } }}
如果我只想打印Person和Person的子类,又不想打印任何类型,希望有一个类型范围,该如何操作呢?
如下,泛型限定
import java.utils.*;class GenericDemo6{ public static void main(String[] args) { ArrayList<Person> a1 = new ArrayList<String>(); a1.add(new Person("abc1")); a1.add(new Person("abc2")); a1.add(new Person("abc3")); printColl(a1); ArrayList<Student> a2 = new ArrayList<Student>(); a2.add(new Student("abc4")); a2.add(new Student("abc5")); a2.add(new Student("abc6")); printColl(a2); //这样既可以打印Person类,也可以打印Person的子类 } public static void printColl(ArrayList<? extends Person> a1) //泛型限定 { Iterator<? extends Person> it = a1.iterator(); while(it.hasNext()) { System.out.println(it.next().getName()); } }}
? 通配符。也叫做占位符。
泛型通配符 ? :任意类型,如果没有明确的,那么就是Object以及任意的Java类了
泛型的限定:
? extends E :可以接受E类型或者E的子类型 。 上限。
? super E : 可以接受E类型或者E的父类型。 下限。
import java.utils.*;class Person{ private String name; Person(String name) { this.name = name } public String name getName() { return name; } public String toString() { return "person :"+name; }}class Student extends Person{ Student(String name) { super(name); }}class Worker extends Person{ Worker(String name) { super(name); }}class GenericDemo7{ public static void main(String[] args) { TreeSe<Student> ts = new TreeSet<Student>(new comp()); ts.add(new Student("abc01")); ts.add(new Student("abc06")); ts.add(new Student("abc02")); Iterator<Student> it = ts.iterator(); while(it.hasNext()) { System.out.println(it.next.getName()); } TreeSe<Worker> ts1 = new TreeSet<Worker>(new comp()); ts1.add(new Worker("wabc01")); ts1.add(new Worker("wabc06")); ts1.add(new Worker("wabc02")); Iterator<Worker> it1 = ts1.iterator(); while(it1.hasNext()) { System.out.println(it1.next.getName()); } }}/*class StuComp implements Comparator<Student>{ public int compara(Student s1,Student s2) { return s1.getName().compareTo(s2.getName()); }}class WorkerComp implements Comparator<Worker>{ public int compara(Worker w1,Worker w2) { return w1.getName().compareTo(w2.getName()); }}*/ 太麻烦,每一个类型都要写一个比较器class Comp implements Comparator<Person> //写一个其父类的比较器 其子类都可以使用{ public int compara(Person p1,Person p2) { return p1.getName().compareTo(p2.getName()); }}
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 泛型
- 在滚动视图上添加的多张图片,清空数组但是还有图片存在界面上,是因为清空的是滚动父控件,子控件并没有销毁,按钮和按钮上的图片还是存在?
- form表单中input标签的11个高级用法
- 下载网络上的图片,并且保存在指定的文件夹中
- linux下tomcat配置开机启动
- 包过滤防火墙
- 泛型
- 牛顿迭代法求解平方根
- BZOJ 3673 && BZOJ 3674 可持久化线段树
- netty 4.0.13 使用样例
- 南邮1340 矩阵逆 初等行转换
- 解析数据出错:reason: '-[__NSCFArray bytes]: unrecognized selector sent to instance 0x18056b30'
- 树莓派3安装QT5
- 趣发现 资源页面请求链接分析
- JS-JavaScript数据类型与对象