wildcard
Thu Feb 15 2024 07:07:16 GMT+0000 (Coordinated Universal Time)
Saved by
@dsce
import java.util.*;
class A{
private int x;
private int y;
public A(int x,int y){
this.x=x;
this.y=y;
}
public int getX(){
return this.x;
}
public int getY(){
return this.y;
}
}
class B extends A{
private int z;
public B(int x,int y,int z)
{
super(x,y);
this.z=z;
}
public int getZ()
{
return this.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.show2(b1);
}
}
content_copyCOPY
Comments