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

📄 ex28(2).java

📁 Java编程宝典Thinking in Java第四版(4th)原书章节后习题及答案
💻 JAVA
字号:
// generics/Ex28.java
// TIJ4 Chapter Generics, Exercise 28, page 685
/* Create a generic class Generic1<T> with a single method that takes an argument
* of type T. Create a second generic class Generic2<T> with a single method that 
* returns an argument of type T. Write a generic method with a contravariant 
* argument of the first generic class that calls its method. Write a second generic
* method with a covariant argument of the second class that calls its method. Test
* using the typeinfo.pets library.
*/
import java.util.*;
import typeinfo.pets.*;

public class Ex28 {
	class Generic1<T> {
		T t;
		void take(T t) { this.t = t; }
	}
	// note that generic type T in Generic1 is not the same as type T in Generic2
	class Generic2<T> {
		T t;
		T give() { return t; }
	}
	<T> void contra(Generic1<? super T> g1t, T t) {
		g1t.take(t);
	}
	<T> T co(Generic2<? extends T> g2t) {
		return g2t.give();
	}
	public static void main(String[] args) {
		Ex28 e28 = new Ex28();
		e28.contra(e28.new Generic1<Pet>(), new Cat("kitty"));
		e28.co(e28.new Generic2<Pet>());		
	}
}

⌨️ 快捷键说明

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