class A{ private int x,y; A(int x,int y){ this.x=x; this.y=y; } public int getX(){ return x; } public int getY(){ return y; } } class B extends A{ private int z; B(int x,int y,int z){ super(x,y); this.z=z; } public int getZ(){ return z; } } class sample<T>{ private T[] values; public sample(T[] values){ this.values=values; } public static <T> void show(sample<? extends A> other){ for(int i=0;i<other.values.length;i++){ System.out.println(other.values[i].getX()+" "+other.values[i].getY()+" "); } } public static <T> void show2(sample<? extends B> other){ for(int i=0;i<other.values.length;i++){ System.out.println(other.values[i].getX()+" "+other.values[i].getY()+" "+other.values[i].getZ()); } } } public class wildcard{ public static void main(String [] args){ A[] ar={new A(1,2),new A(2,3),new A(3,4)}; sample<A> a1=new sample<A>(ar); a1.show(a1); B[] br={new B(1,2,5),new B(2,3,7),new B(3,4,8)}; sample<B> b1=new sample<B>(br); b1.show(b1); } }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter