创建一个范型的类:Pattern.java
public class Pattern<T,K,M> { T cat; K dog; M pig; public Pattern() { this.cat=null; this.dog=null; this.pig=null; } public Pattern(T cat,K dog,M pig) { this.cat=cat; this.dog=dog; this.pig=pig; } public String toString() { return "Cat: "+cat.toString()+" Dog: "+dog.toString()+" Pig: "+pig.toString(); } }
测试这个范型类:PatternTest.java
public class PatternTest { public static void main(String args[]) { Pattern<String,Integer,Integer> s=new Pattern<String,Integer,Integer>(); s.cat="I am a CatString"; s.dog=555; s.pig=999; System.out.println(s); } }
运行结果:
[Jack@win 范型]$ javac PatternTest.java
[Jack@win 范型]$ java PatternTest
Cat: I am a CatString Dog: 555 Pig: 999