標籤:
1.泛型擦除
package cn.itcast.demo;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.ArrayList;import java.util.List;public class Demo1 { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { // 定義一個List<Integer> List<Integer> list = new ArrayList<Integer>(); // 添加一個字串 "abc" // list.add("abc"); // 解決問題,使用反射 // 1.得到List的Class。 Class clazz = list.getClass(); //2.通過clazz去擷取add方法. Method addMethod=clazz.getMethod("add", Object.class); //3.調用add方法,將"abc"添加. addMethod.invoke(list, "abc"); System.out.println(list); }}
2.四種擷取對象的方式.
1.new
2.反射
3.IO流(Object流) ----注意需要實現Serializable
4.clone() ---需要在類中重寫clone()方法實現Cloneable介面.
package cn.itcast.demo;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import org.junit.Test;//擷取對象public class Demo2 { // 1.new @Test public void fun1() { Person p = new Person(); } // 使用反射 @Test public void fun2() throws ClassNotFoundException, InstantiationException, IllegalAccessException { Class c = Class.forName("cn.itcast.demo.Person"); c.newInstance(); } // 3.使用IO流. @Test public void fun3() throws FileNotFoundException, IOException { // 將一個對象寫入到object.txt檔案中. Person p = new Person(); p.setName("tom"); p.setSex("male"); p.setAddress("北京"); // 在java中如果描述路徑要使用 兩個\\,也可以使用一個/。 File file = new File("D:/java0106/workspace/day02/src/object.txt"); // // 絕對路徑 // 相對路徑 // File file=new File("src/object.txt"); // 在開發中擷取classpath路徑。 擷取我們的classpath路徑的根目錄. 那麼如果檔案存在於bin中,就可以直接在後面寫路徑 // String path = Demo2.class.getResource("/object.txt").getPath(); // File file=new File(path); // System.out.println(file.exists()); writeObjectToFile(p, file); } @Test public void fun4() throws FileNotFoundException, IOException, ClassNotFoundException { File file = new File("D:/java0106/workspace/day02/src/object.txt"); Person p = (Person) readObjectFromFile(file); System.out.println(p); } // 從檔案中讀取對象 private Object readObjectFromFile(File file) throws FileNotFoundException, IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); Object obj = ois.readObject(); ois.close(); return obj; } // 將對象寫入到檔案中的操作 private void writeObjectToFile(Object obj, File file) throws FileNotFoundException, IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( file)); oos.writeObject(obj); oos.flush(); oos.close(); } // 使用複製 @Test public void fun5() throws CloneNotSupportedException { Person p = new Person(); p.setName("tom"); p.setSex("male"); p.setAddress("北京"); Person pp = (Person) p.clone(); // 得到一個p對象的副本 System.out.println(pp); }}
2
package cn.itcast.demo;import java.io.Serializable;public class Person implements Cloneable{// implements Serializable { // private static final long serialVersionUID = 1L; private String name; private String sex; private String address; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "Person [name=" + name + ", sex=" + sex + ", address=" + address + "]"; } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); }}
3.自動裝配操作.
建立一個Map集合.
map.put("name","tom");
map.put("sex","男");
map.put("address","北京");
Person類.
class Person{
private String name;
private String sex;
private String address;
get/set方法.
}
要求,使用反射,將map集合中key值與Person類中屬性名稱相同的用key的value對Person類的屬性賦值.
不使用Field類,使用 Method類操作.
package cn.itcast.demo;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.util.HashMap;import java.util.Map;import java.util.Set;import org.junit.Test;public class Demo3 { public static void main(String[] args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException { Map<String, String> map = new HashMap<String, String>(); map.put("name", "tom"); map.put("sex", "男"); map.put("address", "北京"); Person p = new Person(); // 1.得到集合中的所有key Set<String> keys = map.keySet(); // 2.得到所有的Person類中的屬性. Class clazz = p.getClass(); Field[] fields = clazz.getDeclaredFields();// 屬性是private // 3.遍曆屬性,判斷keys是否包含這些屬性. for (Field field : fields) { // 得到屬性名稱. String fieldName = field.getName(); if (keys.contains(fieldName)) { // map集合的key中包含這樣的屬性名稱。那麼就需要調用這個屬性對應的set方法將key對應的value進行賦值操作. // 1.得到value String value = map.get(fieldName); // 2.得到屬性對應的set方法 Method setMethod = clazz.getMethod(getSetMethodName(fieldName), String.class); //3.調用方法進行賦值. setMethod.invoke(p, value); } } System.out.println(p); } public static String getSetMethodName(String param) { return "set" + (char) (param.charAt(0) - 32) + param.substring(1); } @Test public void fun() { // ‘n‘---->N System.out.println((char) (‘n‘ - 32)); }}
泛型擦除 反射 自動裝配