class GenericClass1<T1,T2> // Parameter T1 and T2 must be unique
{
private T1 t1;
private T2 t2;
private T1[] t3;//= new T1[12]; will give error: generic array creation
public void addDataT1(T1 t1) {
this.t1 = t1;
}
public T1 getDataT1() {
return t1;
}
public void addDataT2(T2 t2) {
this.t2 = t2;
}
public T2 getDataT2() {
return t2;
}
public void addDataT3(T1 t1[]) {
this.t3 = t1;
/*for(int i=0;i<3;i++)
System.out.println(t3[i]);*/
}
public T1[] getDataT3() {
return t3;
}
}
class GenericDemo2 {
public static void main(String[] args) {
GenericClass1<Integer,String> student = new GenericClass1<Integer,String>();
student.addDataT1(new Integer(10));
student.addDataT2("SRIMCA");
Integer mark[]={50,40,30};
student.addDataT3(mark);
Integer m[]= new Integer[3];
m=student.getDataT3();
// no cast!
Integer studId = student.getDataT1();
System.out.println("ID="+studId);
String name=student.getDataT2();
System.out.println("Name="+name);
for(int i=0;i<m.length;i++)
System.out.println(m[i]);
}
}
{
private T1 t1;
private T2 t2;
private T1[] t3;//= new T1[12]; will give error: generic array creation
public void addDataT1(T1 t1) {
this.t1 = t1;
}
public T1 getDataT1() {
return t1;
}
public void addDataT2(T2 t2) {
this.t2 = t2;
}
public T2 getDataT2() {
return t2;
}
public void addDataT3(T1 t1[]) {
this.t3 = t1;
/*for(int i=0;i<3;i++)
System.out.println(t3[i]);*/
}
public T1[] getDataT3() {
return t3;
}
}
class GenericDemo2 {
public static void main(String[] args) {
GenericClass1<Integer,String> student = new GenericClass1<Integer,String>();
student.addDataT1(new Integer(10));
student.addDataT2("SRIMCA");
Integer mark[]={50,40,30};
student.addDataT3(mark);
Integer m[]= new Integer[3];
m=student.getDataT3();
// no cast!
Integer studId = student.getDataT1();
System.out.println("ID="+studId);
String name=student.getDataT2();
System.out.println("Name="+name);
for(int i=0;i<m.length;i++)
System.out.println(m[i]);
}
}
No comments:
Post a Comment