java中的泛型 (一)

2014-11-24 09:56:21 · 作者: · 浏览: 2
import java.util.Set;

//dao data access object--->crud
public class GenericDao  {
 public void add(E x){
  
 }
 
 public E findById(int id){
  return null;
 }
 
 public void delete(E obj){
  
 }
 
 public void delete(int id){
  
 } 
 
 public void update(E obj){
  
 }
 
 public static  void update2(E obj){
  
 }
 
 public E findByUserName(String name){
  return null;
 }
 public Set findByConditions(String where){
  return null;
 }
}


-------------------------------------------------------------------

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Vector;

import cn.itcast.day1.ReflectPoint;

public class GenericTest {

 /**
  * @param args
  */
 public static void main(String[] args) throws Exception {
  // TODO Auto-generated method stub
  ArrayList collection1 = new ArrayList();
  collection1.add(1);
  collection1.add(1L);
  collection1.add("abc");
  //int i = (Integer)collection1.get(1);
  
  ArrayList collection2 = new ArrayList();
  //collection2.add(1);
  //collection2.add(1L);
  collection2.add("abc");
  String element = collection2.get(0); 
  
  //new String(new StringBuffer("abc"));
  Constructor constructor1 = String.class.getConstructor(StringBuffer.class);
  String str2 = constructor1.newInstance(/*"abc"*/new StringBuffer("abc"));
  System.out.println(str2.charAt(2));  
  
  ArrayList collection3 = new ArrayList();
  System.out.println(collection3.getClass() == collection2.getClass());
  //collection3.add("abc");
  collection3.getClass().getMethod("add", Object.class).invoke(collection3, "abc");
  System.out.println(collection3.get(0));
  
  printCollection(collection3);
  
  //Class
x = String.class.asSubclass(Number.class); Class< > y; Class x ;//Class.forName("java.lang.String"); HashMap maps = new HashMap(); maps.put("zxx", 28); maps.put("lhm", 35); maps.put("flx", 33); Set> entrySet = maps.entrySet(); for(Map.Entry entry : entrySet){ System.out.println(entry.getKey() + ":" + entry.getValue()); } add(3,5); Number x1 = add(3.5,3); Object x2 = add(3,"abc"); swap(new String[]{"abc","xyz","itcast"},1,2); //swap(new int[]{1,3,5,4,5},3,4); Object obj = "abc"; String x3 = autoConvert(obj); copy1(new Vector(),new String[10]); copy2(new Date[10],new String[10]); //copy1(new Vector(),new String[10]); GenericDao dao = new GenericDao(); dao.add(new ReflectPoint(3,3)); //String s = dao.findById(1); //Vector v1 = new Vector(); Method applyMethod = GenericTest.class.getMethod("applyVector", Vector.class); Type[] types = applyMethod.getGenericParameterTypes(); ParameterizedType pType = (ParameterizedType)types[0]; System.out.println(pType.getRawType()); System.out.println(pType.getActualTypeArguments()[0]); } public static void applyVector(Vector v1){ } private static void fillArray(T[] a,T obj){ for(int i=0;i T autoConvert(Object obj){ return (T)obj; } private static void swap(T[] a,int i,int j){ T tmp = a[i]; a[i] = a