自定义泛型:
//通过类型推出泛型
public class BooleanFoo
{
private Boolean foo;
public Boolean getFoo()
{
return foo;
}
public void setFoo(Boolean foo)
{
this.foo = foo;
}
}
public class IntegerFoo
{
private Integer foo;
public Integer getFoo()
{
return foo;
}
public void setFoo(Integer foo)
{
this.foo = foo;
}
}
public class ObjectFoo
{
private Object foo;
public Object getFoo()
{
return foo;
}
public void setFoo(Object foo)
{
this.foo = foo;
}
public static void main(String[] args)
{
ObjectFoo foo1 = new ObjectFoo();
ObjectFoo foo2 = new ObjectFoo();
foo1.setFoo(new Boolean(false));
Boolean b = (Boolean)foo1.getFoo();
foo2.setFoo(new Integer(10));
Integer i = (Integer)foo2.getFoo();
ObjectFoo foo3 = new ObjectFoo();
foo3.setFoo(new Boolean(false));
String str = (String)foo3.getFoo();
}
}
//基本泛型类型
public class Generic<T1, T2>
{
private T1 foo1;
private T2 foo2;
public T1 getFoo1()
{
return foo1;
}
public void setFoo1(T1 foo1)
{
this.foo1 = foo1;
}
public T2 getFoo2()
{
return foo2;
}
public void setFoo2(T2 foo2)
{
this.foo2 = foo2;
}
public static void main(String[] args)
{
Generic<Integer, Boolean> foo = new Generic<Integer, Boolean>();
foo.setFoo1(new Integer(-20));
foo.setFoo2(new Boolean(false));
System.out.println(foo.getFoo1());
System.out.println(foo.getFoo2());
}
//前面三个类:BooleanFoo、IntegerFoo、ObjectFoo的说明都是为了推出这个GenericFoo泛型类。严格定义。
public class GenericFoo<T>
{
private T foo;
public T getFoo()
{
return foo;
}
public void setFoo(T foo)
{
this.foo = foo;
}
public static void main(String[] args)
{
GenericFoo<Boolean> foo1 = new GenericFoo<Boolean>();
GenericFoo<Integer> foo2 = new GenericFoo<Integer>();
foo1.setFoo(new Boolean(false));
foo2.setFoo(new Integer(3));
Boolean b = foo1.getFoo();
Integer i = foo2.getFoo();
System.out.println(b);
System.out.println(i);
//foo1 = foo2;
GenericFoo a = new GenericFoo();
a.setFoo("hello");
String str = (String)a.getFoo();
System.out.println(str);
}
}
//数组泛型类型
public class Generic2<T>
{
private T[] fooArray;
public T[] getFooArray()
{
return fooArray;
}
public void setFooArray(T[] fooArray)
{
this.fooArray = fooArray;
}
public static void main(String[] args)
{
Generic2<String> foo = new Generic2<String>();
String[] str1 = {"hello", "world", "welcome"};
String[] str2 = null;
foo.setFooArray(str1);
str2 = foo.getFooArray();
for(int i = 0; i < str2.length; i++)
{
System.out.println(str2[i]);
}
}
}
//利用数组模拟ArrayList集合类
public class SimpleCollection<T>
{
private T[] objArr;
private int index = 0;
public SimpleCollection()
{
objArr = (T[])new Object[10]; //此处是数组类型转换
}
public SimpleCollection(int capacity)
{
objArr = (T[])new Object[capacity];
}
public void add(T t)
{
objArr[index++] = t;
}
public int getLength()
{
return this.index;
}
public T get(int i)
{
return objArr[i];
}
public static void main(String[] args)
{
SimpleCollection<Integer> c = new SimpleCollection<Integer>();
for(int i = 0; i < 10; i++)
{
c.add(new Integer(i));
}
for(int i = 0; i < 10; i++)
{
Integer in = c.get(i);
System.out.println(in);
}
}
}
//泛型中包含泛型
public class WrapperFoo<T>
{
private GenericFoo3<T> foo;
public GenericFoo3<T> getFoo()
{
return foo;
}
public void setFoo(GenericFoo3<T> foo)
{
this.foo = foo;
}
public static void main(String[] args)
{
GenericFoo3<Integer> foo = new GenericFoo3<Integer>();
foo.setFoo(new Integer(-10));
WrapperFoo<Integer> wrapper = new WrapperFoo<Integer>();
wrapper.setFoo(foo);
GenericFoo3<Integer> g = wrapper.getFoo();
System.out.println(g.getFoo());
}
}
class GenericFoo3<T>
{
private T foo;
public T getFoo()
{
return foo;
}
public void setFoo(T foo)
{
this.foo = foo;
}
}
//泛型举例:ArrayList<T>
public class ArrayListTest
{
public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
for(int i = 0; i < list.size(); i++)
{
String value = list.get(i);
System.out.println(value);
}
for(Iterator<String> iter = list.iterator(); iter.hasNext();)
{
String value = iter.next();
System.out.println(value);
}
}
}
//泛型举例:HashSet<T>
public class SetTest
{
public static void main(String[] args)
{
Set<String> set = new HashSet<String>();
set.add("aa");
set.add("bb");
set.add("cc");
for (Iterator<String> iter = set.iterator(); iter.hasNext();)
{
String value = iter.next();
System.out.println(value);
}
System.out.println("----------------");
Set<People> set2 = new HashSet<People>();
set2.add(new People("zhangsan", 20, "beijing"));
set2.add(new People("lisi", 30, "shanghai"));
set2.add(new People("wangwu", 40, "tianjin"));
for(Iterator<People> iter = set2.iterator(); iter.hasNext();)
{
People people = iter.next();
String name = people.getName();
int age = people.getAge();
String address = people.getAddress();
System.out.println(name + "," + age + "," + address);
}
}
}
class People
{
private String name;
private int age;
private String address;
public People(String name, int age, String address)
{
this.name = name;
this.age = age;
this.address = address;
}
public int hashCode()
{
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + age;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj)
{
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
People other = (People) obj;
if (address == null)
{
if (other.address != null)
return false;
}
else if (!address.equals(other.address))
return false;
if (age != other.age)
return false;
if (name == null)
{
if (other.name != null)
return false;
}
else if (!name.equals(other.name))
return false;
return true;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getAddress()
{
return address;
}
public void setAddress(String address)
{
this.address = address;
}
}
//泛型举例:HashMap<T>
public class MapTest
{
public static void main(String[] args)
{
Map<String, String> map = new HashMap<String, String>();
map.put("a", "aa");
map.put("b", "bb");
map.put("c", "cc");
map.put("d", "dd");
Set<String> set = map.keySet();
for (Iterator<String> iter = set.iterator(); iter.hasNext();)
{
String key = iter.next();
String value = map.get(key);
System.out.println(key + ":" + value);
}
System.out.println("----------------------------------");
Set<Map.Entry<String, String>> set2 = map.entrySet();
for (Iterator<Map.Entry<String, String>> iter = set2.iterator(); iter
.hasNext();)
{
Map.Entry<String, String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key + ":" + value);
}
}
}
//限制泛型中的类型,不管是接口还是类都是用extends,默认情况下T extends Object
public class ListGenericFoo<T extends List>//在这里可以是List<String>
{
private T[] fooArray;
public T[] getFooArray()
{
return fooArray;
}
public void setFooArray(T[] fooArray)
{
this.fooArray = fooArray;
}
public static void main(String[] args)
{
ListGenericFoo<LinkedList> foo1 = new ListGenericFoo<LinkedList>();
ListGenericFoo<ArrayList> foo2 = new ListGenericFoo<ArrayList>();
LinkedList[] linkedList = new LinkedList[10];
foo1.setFooArray(linkedList);
ArrayList[] arrayList = new ArrayList[10];
foo2.setFooArray(arrayList);
//ListGenericFoo<HashMap> foo3 = new ListGenericFoo<HashMap>();//这是错误的
}
}
//泛型赋值给另一个泛型
public class GenericTest<T>
{
private T foo;
public T getFoo()
{
return foo;
}
public void setFoo(T foo)
{
this.foo = foo;
}
public static void main(String[] args)
{
GenericTest<? extends List> ge = null;
ge = new GenericTest<ArrayList>();
ge = new GenericTest<LinkedList>();
//ge = new GenericTest<HashMap>();
GenericTest<? super List> ge2 = null;
ge2 = new GenericTest<Object>();
GenericTest<String> ge3 = new GenericTest<String>();
ge3.setFoo("hello world");
GenericTest<?> ge4 = ge3;//其实这里的?相当于?extends Object,常用一个文号就行了
System.out.println(ge4.getFoo());
ge4.setFoo(null);
System.out.println(ge4.getFoo());
//ge4.setFoo("welcome");
}
}