准备工作7 b- Z; A6 N* T
4 a7 F' y7 U g8 L% S; X
public class One implements Serializable {! k& ]( A1 O7 p6 B4 g& X5 Q8 M4 D Y
private String user_id;- d q4 Y8 m$ x# C$ }1 v% K5 i
private String name;
4 Z Y* B" w3 T$ y$ o% A( L; ^- C9 N# K7 g
public One() {% M, i: |- E ^" o
}
0 N! J% x6 o8 t- U l7 w# S$ u4 ?3 N
public One(String user_id, String name) { G5 |7 s% f; N1 A3 G
this.user_id = user_id;
* i+ b0 q* X d. o" w( [ this.name = name;2 z. x0 u- b* m6 z3 x" v
}
8 K9 Z1 h2 f6 s' J% ^) P
0 u p" _: u+ Z7 v3 q( E) [ public String getUser_id() {
- S+ }+ \) _' O" T7 p return user_id;
7 j2 H1 ]; i, i- X) C* b1 _ }
# S2 L0 b" a9 G9 L9 S1 F, ^( Y) v4 |( W& C
public void setUser_id(String user_id) {2 \6 ^. ~1 \. M6 E& a. M1 [
this.user_id = user_id;+ | |) U- ~! P$ w
}, ^) r' g1 \9 |
8 N, N n5 _7 o4 @' y4 [( @( l
public String getName() {# X2 E/ P5 q; y; l/ Q: H
return name;
/ s* O1 b& q4 `2 M+ \" n }
% b' a7 w. W/ a; G9 J
$ m3 @. q/ M1 u( }5 U public void setName(String name) {6 i/ }. X' S9 K4 ]# L5 Q
this.name = name;3 W8 c& O5 x' x* H6 T
}0 C2 H0 R) ?$ P
, B; g1 c1 A6 _1 N
@Override$ j+ Y7 X" [% g" W7 o' d
public String toString() {# B4 ]) v$ k+ _5 X! n1 x# W
return "One{" +! s% W+ }5 W0 B( L7 m7 q6 Y7 p
"user_id='" + user_id + '\'' +
% y/ Q+ E8 M; F9 ?. S$ J. W' H ", name='" + name + '\'' +" S: y2 L' t' B" z! ]
'}';- j L( R- n2 p2 x- O
}+ y h/ \1 j% h: b% R& b
, b9 E. ]" a% _. e4 j
@Override
& d- D F% b% i( ^ public One clone() {
% w" w: g5 _& b+ G try {
, L+ `0 p; M7 }& \+ x return (One) super.clone();
! e n& Z2 ~+ H; C& ^/ G4 f, ~ } catch (CloneNotSupportedException e) {
' f+ a1 k. V8 B6 B; v/ f1 \; Q/ ^ e.printStackTrace();
( L$ n7 ?( _5 w }
7 Q0 i/ j8 i3 Q( B( I. E return null;
; m& h/ q: {, v# \3 ` }
- U; H+ I3 ]. U6 A# n/ l3 b) u}<hr/>一、使用new 方式创建对象1 S! A) ]5 J$ _1 _3 l6 g7 I+ Z- S) o
2 D$ O1 ~! j" ] {+ g9 |5 a# `
public static void main(String[] args) {
% i2 {2 }* C: T6 [" u5 C // 无参
' j0 y5 t2 G( E1 a" B) R One one = new One();7 ?0 c2 m/ n" O, I- |* R% [# y6 c
// 有参- l/ Q. C! H% K' i8 K$ S
One one1 = new One("1","tom");
+ A/ k% c; C6 @+ ~% i6 I/ H System.out.println(one);
; L4 H( A; i/ o" O7 s2 b System.out.println(one1);' ^) C" a7 ^8 {' x4 M
}结果:. L7 }3 T1 a* w5 W! H
2 F- ^9 T( m8 w) O5 B; l5 V
( e& I: [% l5 c5 ]" {
二、使用反射机制创建对象- C% i1 k8 A7 n( K1 `
" C* A% E1 t5 s& m; W: E
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, 4 H6 O: X/ {9 u2 Z( D$ y% @
InvocationTargetException, InstantiationException, IllegalAccessException {/ X4 g' D- l- r9 T- C4 `3 K0 y
Class<?> aClass = Class.forName(One.class.getName());
3 p3 V5 |" u2 M Object o = aClass.getDeclaredConstructor().newInstance(); // 无参
* T* L1 G$ i$ S: C6 m Object o1 = aClass.getDeclaredConstructor(String.class, String.class).newInstance("1","tom"); // 有参
+ |2 o4 _1 J1 l7 w System.out.println(o);
. M/ W1 T- c C% D4 q( w) k System.out.println(o1);
, f1 ]: t- T) j0 j. L }结果:
) k# ~$ Z0 Z5 r
: d Z/ c" C: N; m6 b7 _1 Q3 n4 V
& M" l* u' @0 a5 f9 `" g* }% [, H$ P; X B, v2 ? K5 |
四、使用序列化创建对象
2 n9 ?9 O2 F5 ]" @* X7 E7 }0 C4 K7 q, d7 R5 G- N5 k
这种方式其实是根据既有的对象进行复制,这个需要事先将可序列化的对象线存到文件里。
. V* ^; {7 e/ y5 q; g3 k public static void main(String[] args) {
x/ V, B5 D$ ~ o; K$ E One one = new One("1", "2"); `/ v2 X" Q, v/ Q7 @( @( u
// 序列化0 ?! { ^, Y: P
try (FileOutputStream fileOut = new FileOutputStream("object.ser");: k, Q8 ]) J$ {6 o; a3 X9 x" m6 y3 K
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut)) {+ g2 |# k8 L" k. a. K
objectOut.writeObject(one); B) y' a. l2 P8 J; h% r
System.out.println("对象已成功序列化");" q0 A, R+ @" }. ~
} catch (IOException e) {
* R) i) x1 F0 v2 m( C+ M' u e.printStackTrace();
B) K) w; q9 [5 | }, Q5 Z+ _0 \6 H @
1 V) ]( |: A0 e // 使用反序列化后的对象- _% C" x2 G. J# e& @: h) |. ^
try (FileInputStream fileIn = new FileInputStream("object.ser");5 o+ v$ z% c9 i* F+ z1 E* T
ObjectInputStream objectIn = new ObjectInputStream(fileIn)) {$ T* s, S) j: s I' G
Object o = objectIn.readObject();
) k I6 ~( X/ e) Q) u! Y# o: } System.out.println("对象已成功反序列化");
3 T, X' J# E0 K$ b7 x% S System.out.println(o); C2 a/ g. J( o7 v: Q! p. y: `
} catch (IOException | ClassNotFoundException e) {
G) c. k# ?- \7 V9 W( p& [) H e.printStackTrace();3 F" T# C- [ G A( X
}3 G1 D0 s: w& Z5 `. g' V1 Y
}结果:
' n7 d$ {1 W' }& R' N! B- P* r+ b v* g8 {
- Z1 V9 B3 ]7 x# G1 b# t. d6 r& l" p) e6 o; Z
四、深拷贝创建对象- Y1 [& D- ] D) r( Z
) b c1 U4 y& Z! X这种方式其实也是根据既有的对象进行复制,需要实现Cloneable接口,并重写clone接口。
# h8 l c$ V9 q( W& M public static void main(String[] args) {
4 o# d7 x" Y# N3 V L5 T: ] One one = new One("1","2");7 `+ J F7 ?+ n% ]4 c4 o
One clone = one.clone();, N+ i5 g" r1 t2 @/ U, D3 Q
System.out.println(clone);
6 W, J$ ~+ U7 v0 B' @: h& J }结果:
3 w8 n1 T. n( h; y: R& C, i/ D4 D: t+ b1 L; G% S
|