一、序列化
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); }