创建对象的三种方式:
1、new
2、克隆----要实现Cloneable接口
3、反序列化(前提:序列化存储)
1、new
1 package com.wisezone.test; 2 /** 3 * 创建对象的第一种方式:new 4 * @author 王东海 5 * @2017年4月15日 6 */ 7 public class TestNew 8 { 9 public String name; 10 public String pwd; 11 public static void main(String[] args) 12 { 13 TestNew t1 = new TestNew(); 14 t1.name = "zs"; 15 t1.pwd = "123"; 16 System.out.println(t1.name); 17 System.out.println(t1.pwd); 18 } 19 }
2、克隆
1 package com.wisezone.clone; 2 3 /** 4 * 空接口: 5 * 标识,告诉JVM,通行 6 * 1、克隆 7 * 2、序列化 8 * 9 * 创建对象的第二种方式:克隆clone,要实现Cloneable 10 * @author 王东海 11 * @2017年4月15日 12 */ 13 public class TestClone implements Cloneable 14 { 15 public String name; 16 17 public static void main(String[] args) 18 { 19 TestClone t1 = new TestClone(); 20 t1.name = "zs"; 21 System.out.println(t1.name); 22 23 try 24 { 25 TestClone t2 = (TestClone) t1.clone(); 26 t2.name = "ww"; 27 System.out.println(t2.name); 28 } catch (CloneNotSupportedException e) 29 { 30 e.printStackTrace(); 31 } 32 } 33 }
3、反序列化
1 package com.wisezone.test; 2 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 import java.io.IOException; 6 import java.io.ObjectInputStream; 7 import java.io.ObjectOutputStream; 8 import java.io.Serializable; 9 import java.util.Arrays; 10 import java.util.List; 11 /** 12 * 创建对象的第三种方式:使用反序列化 13 * @author 王东海 14 * @2017年4月15日 15 */ 16 public class Book implements Serializable{ 17 18 private static final long serialVersionUID = -6212470156629515269L; 19 20 /**书名*/ 21 private String name; 22 23 /**作者*/ 24 private List<String> authors; 25 26 /**ISBN*/ 27 private String isbn; 28 29 /**价格*/ 30 private float price; 31 32 public Book() 33 { 34 super(); 35 // TODO Auto-generated constructor stub 36 } 37 38 public String getName() 39 { 40 return name; 41 } 42 43 public void setName(String name) 44 { 45 this.name = name; 46 } 47 48 public List<String> getAuthors() 49 { 50 return authors; 51 } 52 53 public void setAuthors(List<String> authors) 54 { 55 this.authors = authors; 56 } 57 58 public String getIsbn() 59 { 60 return isbn; 61 } 62 63 public void setIsbn(String isbn) 64 { 65 this.isbn = isbn; 66 } 67 68 public float getPrice() 69 { 70 return price; 71 } 72 73 public void setPrice(float price) 74 { 75 this.price = price; 76 } 77 78 public static long getSerialversionuid() 79 { 80 return serialVersionUID; 81 } 82 83 @Override 84 public String toString() 85 { 86 return "Book [name=" + name + ", authors=" + authors + ", isbn=" + isbn + ", price=" + price + "]"; 87 } 88 89 public static void main(String[] args) 90 { 91 /** 92 * 5. 使用反序列化 93 */ 94 Book book1 = new Book(); 95 book1.setName("Redis"); 96 book1.setAuthors(Arrays.asList("Eric", "John")); 97 book1.setPrice(59.00f); 98 book1.setIsbn("ABBBB-QQ677868686-HSDKHFKHKH-2324234"); 99 System.out.println(book1); 100 101 try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("book.dat")); 102 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("book.dat"));) { 103 oos.writeObject(book1); 104 105 Book book5 = (Book) ois.readObject(); 106 System.out.println(book5); 107 108 } catch (IOException | ClassNotFoundException e) { 109 e.printStackTrace(); 110 } 111 } 112 113 }