标签:
首先,说说final。
final关键字可以修饰变量,方法,类。
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; }
public final String finalMethod(){ return "Hello World" ; }
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关键字:
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接口的类,所以序列化会自动进行
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编程思想》第四版
transient相关部分参考自:http://www.cnblogs.com/lanxuezaipiao/p/3369962.html
标签:
原文地址:http://www.cnblogs.com/jeyson/p/5601259.html