java 泛型

1.简介

  • java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。
  • 泛型经常被称为参数化类型,它能够像方法一样接受不同类型的参数。

  • 泛型的本质是为了参数化类型(在不创建新的类型的情况下,通过泛型指定的不同类型来控制形参具体限制的类型)。也就是说在泛型使用过程中,操作的数据类型被指定为一个参数,这种参数类型可以用在类、接口和方法中,分别被称为泛型类、泛型接口、泛型方法。

  • 把明确数据类型的工作推迟到了创建对象或者调用方法的时候去做的。

  • 泛型的好处:
     *         A:提高了程序的安全性
     *         B:将运行期遇到的问题转移到了编译期
     *         C:省去了类型强转的麻烦

  • 格式

2.泛型的使用

1.集合在使用上的问题 集合只能存引用数据类型  ,默认下都是Object类型

    ArrayList array = new ArrayList();
        
        array.add("java");
         array.add(100); // 自动装箱为 Integer

        Iterator it = array.iterator();
        while (it.hasNext()) {
            String s = (String) it.next();
            System.out.println(s);

}//运行时出错,报java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String

上面这段定义了一个List类型的集合,先向其中加入了两个字符串类型的值,随后加入一个Integer类型的值。这是完全允许的,因为此时list默认的类型为Object类型。在之后的循环中,由于忘记了之前在list中也加入了Integer类型的值或其他编码原因,很容易出现类似的错误。因为编译阶段正常,而运行时会出现“java.lang.ClassCastException”异常。因此,导致此类错误编码过程中不易发现。

2. 集合使用泛型

public class ArrayListDemo2 {
	public static void main(String[] args) {
		// 存储字符串并遍历
                //JDK7泛型新特性,菱形泛型,泛型推断
		ArrayList<String> array = new ArrayList<String>();

		// 添加集合
		array.add("hello");
		array.add("world");
		array.add("java");
		// array.add(100);编译就报错

		// 遍历
		Iterator<String> it = array.iterator();
		while (it.hasNext()) {
			// String s = (String) it.next();
			String s = it.next();
			System.out.println(s);
		}
	}
}

 

3.泛型接口

/*
 * 泛型接口:把泛型定义在接口上
 * 
 * 格式:
 * 		interface 接口名<泛型类型,...>
 */
public interface Inter<YY> {
	public abstract void show(YY yy);
}

泛型接口实现

/*
 * 实现类在实现泛型接口的时候:
 * A:在实现接口的时候,我已经明确了接口的泛型参数类型
 * B:在实现接口的时候,我还不知道接口的泛型参数类型
 */

//知道泛型参数类型
//public class InterImpl implements Inter<String> {
//	@Override
//	public void show(String yy) {
//		System.out.println(yy);
//	}
// }


//不知道泛型参数类型
public class InterImpl<YY> implements Inter<YY> {
	@Override
	public void show(YY yy) {
		System.out.println(yy);
	}
}

// new InterImpl<String>();

4.泛型类

/*
 * 泛型类:把泛型定义在类上。
 * 格式:
 * 		class 类名<类型,...> {}
 */
public class Tool2<QQ> {
	private QQ qq;

	public QQ getQq() {
		return qq;
	}

	public void setQq(QQ qq) {
		this.qq = qq;
	}
}

5.泛型方法

/*
 * 泛型方法:把泛型添加到方法上。
 * 格式:
 * 		public <泛型类型> 返回类型 方法名(泛型类型 .)
 */
public class Tool3 {

	public <BMW> void print(BMW bmw) {
		System.out.println(bmw);
	}
}

/*
 * 用Object可以让我们的程序提高扩展性。
 * 但是问题是,隐含了类型转换异常。
 * 所以,这种解决方案不是特别的好。
 * 这个时候,java就提供了泛型技术。
 */ 

3.泛型高级

/*
 *泛型高级:
 *泛型通配符<?>
 *        任意类型,如果没有明确,那么就是Object以及任意的Java类了
 *? extends E
 *        向下限定,E及其子类
 *? super E
 *        向上限定,E及其父类
 */

public class GenericeDemo {
	public static void main(String[] args) {
		// 泛型通配符<?>
		Collection<?> c1 = new ArrayList<Animal>();
		Collection<?> c2 = new ArrayList<Dog>();
		Collection<?> c3 = new ArrayList<Cat>();
		Collection<?> c4 = new ArrayList<Object>();
		System.out.println("--------------------");

		// ? extends E
		Collection<? extends Animal> c5 = new ArrayList<Animal>();
		Collection<? extends Animal> c6 = new ArrayList<Dog>();
		Collection<? extends Animal> c7 = new ArrayList<Cat>();
		// Collection<? extends Animal> c8 = new ArrayList<Object>();
		System.out.println("--------------------");

		// ? super E
		Collection<? super Animal> c9 = new ArrayList<Animal>();
		// Collection<? super Animal> c10 = new ArrayList<Dog>();
		// Collection<? super Animal> c11 = new ArrayList<Cat>();
		Collection<? super Animal> c12 = new ArrayList<Object>();
	}
}

class Animal {
}

class Dog extends Animal {
}

class Cat extends Animal {
}

 

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值