Elkezdtem ezzel is foglalkozni, igazabol szegyen, hogy eddig alig foglalkoztam veluk, pedig milyen szep!
Kezdoknek youtube video:
Es ha mar kicsit PRO-t is akarsz:
http://www.angelikalanger.com/GenericsFAQ/JavaGenericsFAQ.pdf
Vigyazat, 350 oldal!:)))
Kis sajat kod:
public class Generics {
interface IParent {
public void printX();
public void printASD();
public <T> void printABC(T s);
}
@Author(first = "Gipsz", last = "Jakab")
static abstract class BaseParentClass implements IParent {
@Override
public abstract void printX();
@Override
public void printASD() {
System.out.println("printASD " + this.toString());
}
@Override
public <T> void printABC(T t) {
System.out.println("printABC " + t.getClass().getSimpleName() );
}
}
static class ParentClass extends BaseParentClass {
ParentClass() {
}
@Override
public String toString() {
return this.getClass().getSimpleName();
}
@Override
public void printX() {
System.out.println("printX " + this.toString());
}
}
static class ChildClass extends ParentClass {
ChildClass() {
super();
}
public void printY() {
System.out.println("printY " + this.toString());
}
@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
static class ChildChildClass extends ChildClass {
ChildChildClass() {
super();
}
public void printY() {
System.out.println("printY " + this.toString());
}
@Override
public String toString() {
return this.getClass().getSimpleName();
}
}
private static <T> void printMe(T[] array) {
for (T t: array) {
System.out.printf("printMe is: %s %s \n", t, (t.getClass().getSimpleName()) );
}
System.out.println("----------------------");
}
private static void printTestArray1(ArrayList<? extends ParentClass> array) {
for (ParentClass c: array) {
System.out.printf("printTestArray1 is a %s\n", c.toString() );
c.printX();
}
System.out.println("----------------------");
}
private static void printTestArray2(ArrayList<? super ChildClass> array) {
for (Object c: array) {
System.out.printf("printTestArray2 is a %s\n", c.toString() );
((ChildClass) c).printY();
}
System.out.println("----------------------");
}
private static void printTestArray3(ArrayList<?> array) {
for (Object c: array) {
System.out.printf("printTestArray3 is a %s\n", c.toString() );
}
System.out.println("----------------------");
}
public static void testCases() {
String[] sArr = {"one", "twoo", "three"};
Character[] cArr = {'A', 'B', 'C'};
Integer[] iArr = {100, 200, 300};
printMe(sArr);
printMe(cArr);
printMe(iArr);
System.out.println("----------------------");
////////////////////
ArrayList<ParentClass> test1 = new ArrayList<ParentClass>();
test1.add(new ParentClass());
test1.add(new ChildClass());
printTestArray1(test1);
ArrayList<ChildClass> test2 = new ArrayList<ChildClass>();
test2.add(new ChildClass());
test2.add(new ChildChildClass());
printTestArray2(test2);
printTestArray3(test1);
Test1 t1 = MyTests.getInctance().new Test1() {
@Override
public void start() {
super.start();
System.out.println("start2");
}
};
t1.start();
System.out.println("----------------------");
System.out.printf("maximum is: %s\n", getMaximum(1, 50, 34));
System.out.println("----------------------");
System.out.printf("maximum is: %s\n", getMaximum("asd", "rtt", "ddd"));
System.out.println("----------------------");
System.out.printf("comparePairs is: %s\n", comparePairs(pair1, pair2));
System.out.println("----------------------");
System.out.printf("wildString is: %s\n", wildString(pair3));
//NumberList<?> nList = new NumberList<Integer>();
//nList.setValues(values);
}
private static <T extends Comparable<T>> T getMaximum(T a, T b, T c) {
T m = a;
if (b.compareTo(m)>0) m = b;
if (c.compareTo(m)>0) m = c;
return m;
}
private static Pair<String, Integer> pair1 = new Pair<>("asd1", 1);
private static Pair<String, Integer> pair2 = new Pair<>("asd2", 5);
private static Pair<String, Object> pair3 = new Pair<>("asd3", null);
private static class Pair<K, V> {
K key;
V value;
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public void setKey(K key) {
this.key = key;
}
public V getValue() {
return value;
}
public void setValue(V value) {
this.value = value;
}
}
private static <K, V> boolean comparePairs(Pair<K, V> p1, Pair<K, V> p2) {
return p1.equals(p2);
}
private static String wildString(Pair<String, ? super Number> p) {
return p.getKey();
}
private static Point<Float, Integer> point1 = new Point<>(2.4f, 1);
private static class Point<X extends Number, Y extends Number> {
X x;
Y y;
public Point(X x, Y y) {
this.x = x;
this.y = y;
}
public X getX() {
return x;
}
public void setX(X x) {
this.x = x;
}
public Y getY() {
return y;
}
public void setY(Y y) {
this.y = y;
}
}
private static class NumberList<T extends Number> extends ArrayList<T> {
private List<? extends T> values;
public List<? extends T> getValues() {
return values;
}
public void setValues(List<? extends T> values) {
this.values = values;
}
}
}