一、序列化
1、序列化是對象持久的一種手段,使用java對象序列化,可以將對象保存爲一組字節,並且可以通過反序列化,將這組字節組裝成原有的對象實例。
2、在java中,只要一個類實現了java.io.Serializable接口,那麼它就可以序列化。
3、序列化過程中jvm虛擬機不會關注靜態變量。
4、Transient修飾的變量可以被阻止序列化到文件中,當執行反序列化時,該變量會被賦予默認值。
例1、Student實例
import java.io.Serializable; public class Student implements Serializable{ public String name; private transient int age; private String address; private String tel; Student(String name, int age, String address, String tel){ this.name = name; this.age = age; this.address = address; this.tel = tel; } public void setName(String name){ this.name = name; } public String getName(){ return name; } public void setAge(int age){ this.age = age; } public int getAge(){ return age; } public void setAddress(String address){ this.address = address; } public String getAddress(){ return address; } public void setTel(String tel){ this.tel = tel; } public String getTel(){ return tel; } public String toString(){ return "姓名:" + name + "\n" + "年齡:" + age + "\n" + "地址:" + address + "\n" + "電話:" + tel; } }例2、序列化實例
import java.io.*; import java.util.ArrayList; public class Test13 { public static void main(String args[]){ Student student = new Student("吳",22,"泗洪","18032111311"); ArrayList arrayList = new ArrayList(); arrayList.add("1"); arrayList.add("2"); int a[] = {3,2,6}; System.out.println(student); try { FileOutputStream file = new FileOutputStream("hello"); ObjectOutputStream out = new ObjectOutputStream(file); out.writeObject(student); // out.writeObject(a); // out.writeObject(arrayList); out.flush(); out.close(); file.close(); } catch (FileNotFoundException e){ } catch (IOException e){ } finally { System.out.println("序列化成功"); } } }例3、反序列化,並將對象寫入文件
import java.io.*; import java.util.ArrayList; import java.util.Iterator; public class Test14 { public static void main(String ags[]){ Student s = null; ArrayList array = null; int arr[] = null; File file1 = new File("D:\\my.txt"); try { FileInputStream fil = new FileInputStream("hello"); ObjectInputStream in = new ObjectInputStream(fil); s = (Student) in.readObject();//獲取hello字節文件中的Student實例 // arr = (int[])in.readObject(); // array = (ArrayList)in.readObject(); in.close(); fil.close(); file1.createNewFile();//創建my.txt文本文檔 FileWriter fileWriter = new FileWriter("D:\\my.txt",true); PrintWriter pw = new PrintWriter(fileWriter); pw.print(s);//將Student實例輸出到my.txt文本文檔中 pw.close(); fileWriter.close(); } catch (FileNotFoundException e){ } catch (IOException e){ } catch (ClassNotFoundException e){ } finally { System.out.println("反序列化成功"); System.out.println(s); // Iterator it = array.iterator(); // while (it.hasNext()){ // System.out.println(it.next()); // } // for (int i = 0; i<arr.length; i++){ // System.out.println(arr[i]); // } } } }
二、泛型
1、泛型、即“參數化類型”,在不創建新的類型的情況下,通過泛型指定的不同類型來控制形參具體限制的類型,也就是說在泛型的使用過程中,操作的數據類型被指定爲一個參數,這種參數類型可以用在類、接口和方法中,分別稱爲泛型類、泛型接口、泛型方法。
2、<? extends T> 表示該通配符所代表的類型是T類型的子類,存儲數據受限,獲取的數據只能是Object。
<? super T>表示該通配符所代表的類型是T類型的父類,獲取數據受限。
<?>表示無界通配符,既不能存也不能取,只能存null。
3、使用通配符會擦除原有的類型。
例1:
public class Test15<T> implements Test16<T>{//泛型類 T name; T age; T tel; Test15(T name,T age, T tel){ this.name = name; this.age = age; this.tel = tel; } public T f2(T e){ return e; } public <V>void f1(V v){//泛型方法 System.out.println(v); } public static<E> void f(E e){//泛型方法 System.out.println(e); } public static void main(String []args){ Test15 test15 = new Test15(1,20,"13323232323"); Test15.f("hello"); } }例2:
public interface Test16 <T>{//泛型接口 public abstract T f2(T t); public abstract <T> void f1(T t); }