Sleepy Generics
parent
1e9d90d12a
commit
7da3542ede
Binary file not shown.
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.gemericrules;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class GenericRules10 {
|
||||
public static void f10(List<? extends A> list1, List<? super C> list2) {
|
||||
System.out.println(list1.size());
|
||||
//list1.add(new B());
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
ArrayList<A> list1 = new ArrayList<A>();
|
||||
list1.add(new A());
|
||||
System.out.println(list1.get(0));
|
||||
ArrayList<Object> list2 = new ArrayList<>();
|
||||
f10(list1, list2);
|
||||
|
||||
ArrayList<Integer> list3 = new ArrayList();
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.gemericrules;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class GenericRules10b {
|
||||
|
||||
public static <T> T f10b(A<? extends T>[] ar)
|
||||
{
|
||||
A<? extends T> temp = ar[0];
|
||||
ar[0] = ar[1];
|
||||
ar[1] = temp;
|
||||
|
||||
for (int i = 0; i < ar.length; ++i) {
|
||||
System.out.println(ar[i]);
|
||||
return ar[i].getT();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
A<Integer>[] ar = new A[3];
|
||||
ar[0] = new A(1);
|
||||
ar[1] = new A(2);
|
||||
ar[2] = new A(3);
|
||||
Integer i = f10b(ar);
|
||||
System.out.println("--------");
|
||||
B<String>[] ar1 = new B[2];
|
||||
ar1[0] = new B("John", 45.6); ar1[1] = new B("Mary", new A<Integer>(888));
|
||||
Object o = f10b(ar1); System.out.println("o");
|
||||
System.out.println("------------");
|
||||
}
|
||||
}
|
||||
|
||||
class A<T> {
|
||||
|
||||
private T t;
|
||||
|
||||
public A()
|
||||
{
|
||||
}
|
||||
|
||||
public T getT()
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
public void setT(T t)
|
||||
{
|
||||
this.t = t;
|
||||
}
|
||||
|
||||
void methodThatTakesParameterT(T t)
|
||||
{
|
||||
System.out.println("method p");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "A{" + "t=" + t + "}";
|
||||
}
|
||||
}
|
||||
|
||||
class B<E> extends A {
|
||||
|
||||
private E e;
|
||||
|
||||
public B(E e, Object t)
|
||||
{
|
||||
super(t);
|
||||
this.e = e;
|
||||
}
|
||||
|
||||
public E getE()
|
||||
{
|
||||
return e;
|
||||
}
|
||||
|
||||
public void setE(E e)
|
||||
{
|
||||
this.e = e;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "B{" + "e=" + e + '}';
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.gemericrules;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class GenericsRule9 {
|
||||
public static void f9(List<? super C> list) {
|
||||
System.out.println(list.size());
|
||||
}
|
||||
public static void main(String[] args)
|
||||
{
|
||||
ArrayList<A> list1 = new ArrayList<>();
|
||||
list1.add(new A());
|
||||
f9(list1);
|
||||
System.out.println(list1.get(0));
|
||||
|
||||
ArrayList<Object> list2 = new ArrayList();
|
||||
f9(list2);
|
||||
|
||||
ArrayList<Integer> list3 = new ArrayList();
|
||||
//f9(list3);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.gemericrules;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GenericsRules11 {
|
||||
|
||||
public static void f(List<List<Integer>> table)
|
||||
{
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
List<Integer> row = new ArrayList<Integer>();
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
row.add(i + j);
|
||||
table.add(row);
|
||||
}
|
||||
}
|
||||
|
||||
for (List<Integer> row : table) {
|
||||
System.out.println(row);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
f( new ArrayList<List<Integer>>() );
|
||||
//f( new List<ArrayList<Integer>>() );//will not compile //f( new ArrayList<ArrayList<Integer>>() ); //will not compile /f( new List<List<Integer>>() ); //will not compile
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue