ex20(1).java

来自「thinking in java 经典书的习题答案 希望对大家有帮助啊」· Java 代码 · 共 33 行

JAVA
33
字号
// generics/Ex20.java
// TIJ4 Chapter Generics, Exercise 20, page 654
/* Create an interface with two methods, and a class that implements that interface
* and adds another method. In another class, create a generic method with an 
* argument type that is bounded by the interface, and show that the methods in the 
* interface are callable inside this generic method. In main(), pass an instance of
* the implementing class to the generic method.
*/

interface A {
	void f();
	void g();
}

class A3 implements A {
	public void f() { System.out.println("A3.f()"); }
	public void g() { System.out.println("A3.g()"); }
	public void h() { System.out.println("A3.h()"); }
}

class G {
	public static <T extends A> void j(T x) { 
		x.f();
		x.g();
	}
}

public class Ex20 {
	public static void main(String[] args) {
		A3 a3 = new A3();
		G.j(a3);	
	}
}

⌨️ 快捷键说明

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