首先,說說final。
final關鍵字可以修飾變量,方法,類。
final變量:
需求:
1 需要一個永不改變的編譯時常量
2 一個運行時被初始化的值,不希望被更改
好處:編譯時就執行的計算,減輕運行時的負擔
擴展:
可以修飾基本類型和引用對象。修飾基本類型的時候,表示數值很定不變。修飾對象引用的時候,一旦引用被初始化指向一個對象,就無法再將它更改指向另一個對象(該對象本身是可以修改的)
空白final
final修飾但又沒有給出初始值的域
必須在域的的定義或構造器內用表達式給final賦值(final使用前必須初始化)
注意:
如果一個對象被static和final同時修飾(編譯期常量),一般用大寫表示,下劃線鏈接單詞
修飾參數:
如果final修飾參數,表示該參數可讀,但無法修改。
用法示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
private Random rand= new Random(); private static Random random= new Random(); private final int n1= 12 ; private final int number=rand.nextInt( 30 ); private static final int NUMBER2=random.nextInt( 40 ); @Test public void finalDataTest(){ System.out.println(n1); System.out.println( "--------------------" ); System.out.println(rand.nextInt( 30 )); System.out.println( "--------------------" ); System.out.println( "編譯初始之后,不會改變:" +number); System.out.println( "--------------------" ); System.out.println( "編譯初始之后,不會改變:" +NUMBER2); } /** * final修飾參數:該參數可讀,但無法修改。 * @param sk * @return */ public String finalParam( final String sk){ //sk="jeyson"; final參數不能被修改 return sk; } |
final方法:
final也可以修飾方法,表示該方法不能被子類繼承。
使用final的好處:
1 JDK1.5以前,效率更高,JDK1.5以后可以忽略
2 方法鎖定,確保子類中該方法含義不變,不能被覆蓋
用法示例:
1
2
3
4
5
|
public final String finalMethod(){ return "Hello World" ; } |
final類:
不希望被任何類繼承,可以使用final修飾類
用法示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
public final class FinalClassTx { private int k ; public void getMyWord(){ System. out .println( "這是一個final類,k的值是" +getK()); } public int getK() { return k ; } public void setK( int k) { this .k = k; } } |
然后transient關鍵字:
transient只能修飾變量,表示該變量不能被序列化。
一般我們繼承Serializable接口的類,序列化會自動進行,使用transient修飾的變量在該類被序列化的時候,不會序列化到指定目的地。
所以,
1 被transient修飾的變量不再是對象持久化的一部分,該變量內容序列化無法獲得訪問
2 transient只能修飾變量,不能修飾方法和類
3 一個靜態變量無論是否被transient修飾,都不能被序列化
用法示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
public class TransientEx { public static void main(String[] args) { User user= new User(); user.setUsername( "jeyson" ); user.setPassword( "123456" ); System.out.println( "序列化前:" ); System.out.println( " username=" +user.getUsername()); System.out.println( " password=" +user.getPassword()); //序列化 try { ObjectOutputStream os= new ObjectOutputStream( new FileOutputStream( "C://MyResource//test1.txt" )); os.writeObject(user); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } //反序列化 try { ObjectInputStream is= new ObjectInputStream( new FileInputStream( "C://MyResource//test1.txt" )); user=(User) is.readObject(); is.close(); System.out.println( "序列化后:" ); System.out.println( " username=" +user.getUsername()); System.out.println( " password=" +user.getPassword()); } catch (Exception e) { e.printStackTrace(); } System.out.println( "--------------------------------" ); } } class User implements Serializable{ private static final long serialVersionUID = 1L; private String username; //使用 transient private transient String password; public String getUsername() { return username; } public void setUsername(String username) { this .username = username; } public String getPassword() { return password; } public void setPassword(String password) { this .password = password; } } |
擴展:Externalizable
實現了serializable接口的類,所以序列化會自動進行
實現了Externaliazble接口的類,沒有任何東西可以自動序列化,無論是否使用transient對結果都沒有影響。
此時如果需要序列化,需要在writeExternal方法中上進行手動指定所要序列化的變量。
使用示例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
public class ExternalizableEx implements Externalizable { private transient String name= "ssss" ; @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { name=(String) in.readObject(); } @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(name); } public String getName() { return name; } public void setName(String name) { this .name = name; } public static void main(String[] args) { ExternalizableEx ex= new ExternalizableEx(); ex.setName( "jeyson" ); System.out.println( "Externalizable序列化前:" ); System.out.println(ex.getName()); //序列化 try { ObjectOutputStream os= new ObjectOutputStream( new FileOutputStream( new File( "C://MyResource//test2.txt" ))); os.writeObject(ex); os.flush(); os.close(); } catch (Exception e) { e.printStackTrace(); } //反序列化 try { ObjectInputStream is= new ObjectInputStream( new FileInputStream( new File( "C://MyResource//test2.txt" ))); ex=(ExternalizableEx) is.readObject(); is.close(); System.out.println( "Externalizable序列化后:" ); System.out.println(ex.getName()); } catch (Exception e) { e.printStackTrace(); } } } |
聲明:
final大部分來自《java編程思想》第四版
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。