《core JAVA for the impatient》阅读笔记(5) 泛型编程

泛型方法

类型参数要放在修饰符之后,返回类型之前

public static <T> void swap(T[] array,int i,int j)

调用时:

String[] friends = ...;
Arrays.swap(friends,0,1)

编译器可以推断T应是String类型

类型限定

public static <T extends AutoCloseable> void closeAll(ArrayList<T> elems) throws Exception{
	for(T elem:elems) 
		elem.close();
}

类型变异和通配符

  • 协变性
    可以将 Manager[] 数组传递给参数为 Employee[] 的方法
    因Manager[]是 Employee[] 的子类型

但是,ArrayList<Manager>不是ArrayList<Employee>的子类型
这时不允许转换

  • 子类型通配符
    public static void printNames(ArrayList<? extends Employee> staff)

"?"表示一个未知的Employee子类型

可以给这个方法传递一个Employee类型或者它的子类组成的数组列表

这种方法只能读不能写,因为可以将未知的传入类型转换成Employee类型,但不能将任何对象转换为一个未知的<?extends Employee>

  • 父类型通配符
    ? super Employee代表任意一个Employee的父类类型(包括Object)
    这种通配符经常用作函数式参数
    Predicate<T>接口有一个方法,用于测试T类型的对象是否有特定的属性
public interface Predicate<T>{
	boolean test(T arg);
	...
}
public static void printAll(Employee[] staff,Predicate<? super Employee> filter){
	for(Employee e:staff)
		if(filter.test(e))
			System.out.println(e.getName());
}
  • 带类型变量的通配符

3个例子:

  1. public static <T> void printAll(T[] elements,Predicate<? super T> filter)
  2. public void addAll(Collection<? extends E> c)
  3. public static <T extends Comparable<? super T>> void sort(List<T> list)
  • 无限定通配符和通配符捕获
import java.util.ArrayList;



public class 枚举类型 {
	public static void swap(ArrayList<?> elements,int i,int j){
		swapHelper(elements,i,j);
	}
	private static <T> void swapHelper(ArrayList<T> elements,int i,int j){
		T temp=elements.get(i);
		elements.set(i,elements.get(j));
		elements.set(j,temp);
	}
	public static void main(String[] args) {

	ArrayList<String> ele=new ArrayList<>();
	ele.add("dasd");
	ele.add("daaaa");
	swap(ele,0,1);
	System.out.println(ele);
	}
}

输出结果:[daaaa,dasd]

The release of Java SE 8 introduced significant enhancements that impact the Core Java technologies and APIs at the heart of the Java platform. Many old Java idioms are no longer required and new features like lambda expressions will increase programmer productivity, but navigating these changes can be challenging., , Core Java® for the Impatient is a complete but concise guide to Java SE 8. Written by Cay Horstmann—the author of Java SE 8 for the Really Impatient and Core Java™, the classic, two-volume introduction to the Java language—this indispensable new tutorial offers a faster, easier pathway for learning the language and libraries. Given the size of the language and the scope of the new features introduced in Java SE 8, there’s plenty of material to cover, but it’s presented in small chunks organized for quick access and easy understanding., , If you’re an experienced programmer, Horstmann’s practical insights and sample code will help you quickly take advantage of lambda expressions (closures), streams, and other Java language and platform improvements. Horstmann covers everything developers need to know about modern Java, including, - Crisp and effective coverage of lambda expressions, enabling you to express actions with a concise syntax, - A thorough introduction to the new streams API, which makes working with data far more flexible and efficient, - A treatment of concurrent programming that encourages you to design your programs in terms of cooperating tasks instead of low-level threads and locks, - Up-to-date coverage of new libraries like Date and Time, - Other new features that will be especially valuable for server-side or mobile programmers, Whether you are just getting started with modern Java or are an experienced developer, this guide will be invaluable for anyone who wants to write tomorrow’s most robust, efficient, and secure Java code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值