1 Package cn.itcast_06; 2 3 /* 4 * Generic interface: Define generics on the interface 5 */ 6 Public Interface Inter<t> {7public abstractvoid show (T t); 8 }
1 Packagecn.itcast_06;2 3 //Implementing a class when implementing an interface4 //The first case: you already know what the type is.5 6 //Public class Interimpl implements inter<string> {7 //8 //@Override9 //Public void Show (String t) {Ten //System.out.println (t); One // } A // } - - //The second case: I don't know what type it is. the Public classInterimpl<t>ImplementsInter<t> { - - @Override - Public voidShow (T t) { + System.out.println (t); - } +}
1 Packagecn.itcast_06;2 3 Public classInterdemo {4 Public Static voidMain (string[] args) {5 //test of the first case6 //inter<string> i = new Interimpl ();7 //i.show ("Hello");8 9 // //test of the second caseTeninter<string> i =NewInterimpl<string>(); OneI.show ("Hello"); A -Inter<integer> II =NewInterimpl<integer>(); -Ii.show (100); the } -}
Android (Java) Learning Note 91: Overview and use of generic interfaces