首先,說說final。
final關(guān)鍵字可以修飾變量,方法,類。
final變量:
需求:
1 需要一個永不改變的編譯時常量
2 一個運行時被初始化的值,不希望被更改
好處:編譯時就執(zhí)行的計算,減輕運行時的負(fù)擔(dān)
擴展:
可以修飾基本類型和引用對象。修飾基本類型的時候,表示數(shù)值很定不變。修飾對象引用的時候,一旦引用被初始化指向一個對象,就無法再將它更改指向另一個對象(該對象本身是可以修改的)
空白final
final修飾但又沒有給出初始值的域
必須在域的的定義或構(gòu)造器內(nèi)用表達(dá)式給final賦值(final使用前必須初始化)
注意:
如果一個對象被static和final同時修飾(編譯期常量),一般用大寫表示,下劃線鏈接單詞
修飾參數(shù):
如果final修飾參數(shù),表示該參數(shù)可讀,但無法修改。
用法示例:
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修飾參數(shù):該參數(shù)可讀,但無法修改。 * @param sk * @return */ public String finalParam(final String sk){ //sk="jeyson"; final參數(shù)不能被修改 return sk; } final方法:
final也可以修飾方法,表示該方法不能被子類繼承。
使用final的好處:
1 JDK1.5以前,效率更高,JDK1.5以后可以忽略
2 方法鎖定,確保子類中該方法含義不變,不能被覆蓋
用法示例:
public final String finalMethod(){ return "Hello World" ; }final類:
不希望被任何類繼承,可以使用final修飾類
用法示例:
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關(guān)鍵字:
transient只能修飾變量,表示該變量不能被序列化。
一般我們繼承Serializable接口的類,序列化會自動進(jìn)行,使用transient修飾的變量在該類被序列化的時候,不會序列化到指定目的地。
所以,
1 被transient修飾的變量不再是對象持久化的一部分,該變量內(nèi)容序列化無法獲得訪問
2 transient只能修飾變量,不能修飾方法和類
3 一個靜態(tài)變量無論是否被transient修飾,都不能被序列化
用法示例:
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
實現(xiàn)了serializable接口的類,所以序列化會自動進(jìn)行
實現(xiàn)了Externaliazble接口的類,沒有任何東西可以自動序列化,無論是否使用transient對結(jié)果都沒有影響。
此時如果需要序列化,需要在writeExternal方法中上進(jìn)行手動指定所要序列化的變量。
使用示例:
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編程思想》第四版
參考文章://m.survivalescaperooms.com/article/86996.htm
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點
疑難解答
圖片精選