⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 apply40.java

📁 JAVA编程思想第四版英文原版习题答案. pdf原版的
💻 JAVA
字号:
// generics/Apply40.java
// {main: ApplyTest40}
// TIJ4 Chapter Generics, Exercise 40, page 731
// Add a speak() method to all the pets in tyepinfo.pets. Modify Apply.java to call the speak
// method for a heterogeneous collection of Pet.
import java.lang.reflect.*;
import java.util.*;
import static net.mindview.util.Print.*;
import typeinfo.pets.*;

public class Apply40 {
	public static <T,S extends Iterable<? extends T>> void apply(S seq, Method f, Object... args) {
		try {
			for(T t : seq)
				f.invoke(t, args);
		} catch(Exception e) {
			// Failures are programmer errors
			throw new RuntimeException(e);
		}
	}
}

class FilledList<T> extends ArrayList<T> {	
	public FilledList(Class<? extends T> type, int size) {
		try {
			for(int i = 0; i < size; i++)
				// Assumes default constructor:
				add(type.newInstance());
		} catch(Exception e) {
			throw new RuntimeException(e);
		}
	}
}

class ApplyTest40 {
	public static void main(String[] args) throws Exception {
		// Random list of 10 pets:
		List<Pet> pets = Pets.arrayList(10);
		print("Random pets: " + pets);
		// Applies the overridden methods:
		Apply40.apply(pets, Pet.class.getMethod("speak"));
		// FilledList contains only one type:
		Apply40.apply(new FilledList<Pet>(Dog.class, 5), Pet.class.getMethod("speak"));
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -