Java Transient Keyword Tutorial – ตัวแปรชั่วคราวคือตัวแปรที่ไม่สามารถทำให้เป็นอนุกรมได้
เผยแพร่แล้ว: 2020-08-23 หากคุณมีข้อกำหนดในการทำให้เป็นอนุกรมของออบเจ็กต์ คุณมีตัวเลือกที่จะข้ามการทำให้เป็นอนุกรมของฟิลด์เฉพาะโดย marking it as transient
transient
เป็นคีย์เวิร์ด Java ซึ่งทำเครื่องหมายตัวแปรสมาชิกว่าจะไม่ถูกซีเรียลไลซ์เมื่อยังคงอยู่ในสตรีมของไบต์
คุณต้องทำให้เป็นอันดับวัตถุเมื่อใด
เมื่ออ็อบเจ็กต์ Java ถูกถ่ายโอนผ่านเครือข่าย อ็อบเจ็กต์ needs to be serialized
การทำให้เป็นอันดับแปลงสถานะอ็อบเจ็กต์เป็นไบต์อนุกรม
ลองดูตัวอย่าง Java ชั่วคราว
- ขั้นแรก ให้สร้างวัตถุ POJO CrunchifyTransientVariableObject ซึ่งใช้งาน Serializable
- สร้างคลาสหลัก CrunchifyJavaTransientFieldTutorial
- สร้างวัตถุ
- เขียนวัตถุลงในไฟล์ สตรีมเอาท์พุตไฟล์เป็นสตรีมเอาต์พุตสำหรับเขียนข้อมูลไปยังไฟล์หรือไปยัง FileDescriptor
- อ่านวัตถุ
- หากฟิลด์เป็นแบบชั่วคราว มันจะคืนค่า 0 (สำหรับ int), null (สำหรับฟิลด์สตริง) และอื่นๆ
CrunchifyTransientVariableObject.java
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 |
package crunchify . com . tutorial ; import java . io . Serializable ; // Serializability of a class is enabled by the class implementing the java.io.Serializable interface. // Warning: Deserialization of untrusted data is inherently dangerous and should be avoided. // Untrusted data should be carefully validated according to the "Serialization and Deserialization" section of the . describes best practices for defensive use of serial filters. public class CrunchifyTransientVariableObject implements Serializable { private static final long serialVersionUID = 1L ; private String companyName ; private int employeeCount ; // A transient variable is a variable that can not be serialized. // Variables may be marked transient to indicate that they are not part of the persistent state of an object. private transient int homeZip ; // private int homeZip; public String getName ( ) { return companyName ; } public void setName ( String companyName ) { this . companyName = companyName ; } public int getEmployeeCount ( ) { return employeeCount ; } public void setEmployeeCount ( int employeeCount ) { this . employeeCount = employeeCount ; } public int getZip ( ) { return homeZip ; } public void setZip ( int homeZip ) { this . homeZip = homeZip ; } public CrunchifyTransientVariableObject ( String companyName , int employeeCount , int homeZip ) { this . companyName = companyName ; this . employeeCount = employeeCount ; this . homeZip = homeZip ; } } |
ที่นี่ homeZip
เป็นตัวแปรชั่วคราว
กระทืบJavaTransientFieldTutorial.java
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 |
package crunchify . com . tutorial ; import java . io . * ; /** * @author Crunchify.com * * Java Transient Keyword Tutorial - A transient variable is a variable that can not be serialized. * */ public class CrunchifyJavaTransientFieldTutorial { public static void main ( String [ ] args ) { CrunchifyTransientVariableObject crunchifyCompany = new CrunchifyTransientVariableObject ( "Crunchify.com" , 5 , 95112 ) ; // A file output stream is an output stream for writing data to a File or to a FileDescriptor. try ( FileOutputStream crunchifyFOS = new FileOutputStream ( "crunchify.obj" ) ; ObjectOutputStream crunchifyOOS = new ObjectOutputStream ( crunchifyFOS ) ) { crunchifyOOS . writeObject ( crunchifyCompany ) ; crunchifyOOS . flush ( ) ; } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } CrunchifyTransientVariableObject crunchifyResult = null ; // A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment. try ( FileInputStream crunchifyFOS = new FileInputStream ( "crunchify.obj" ) ; ObjectInputStream crunchifyOIS = new ObjectInputStream ( crunchifyFOS ) ) { crunchifyResult = ( CrunchifyTransientVariableObject ) crunchifyOIS . readObject ( ) ; } catch ( FileNotFoundException e ) { e . printStackTrace ( ) ; } catch ( IOException e ) { e . printStackTrace ( ) ; } catch ( ClassNotFoundException e ) { e . printStackTrace ( ) ; } System . out . println ( "Company Name: " + crunchifyResult . getName ( ) ) ; System . out . println ( "Number of Employee: " + crunchifyResult . getEmployeeCount ( ) ) ; System . out . println ( "Zip code: " + crunchifyResult . getZip ( ) ) ; } } |

เรียกใช้โปรแกรม:
ด้วยคีย์เวิร์ดชั่วคราว:
1 2 3 4 5 6 |
/ Library / Java / JavaVirtualMachines / jdk - 14.0.2.jdk / Contents / Home / bin / java - javaagent Company Name : Crunchify . com Number of Employee : 5 Zip code : 0 Process finished with exit code 0 |
ไม่มีคีย์เวิร์ดชั่วคราว:
ลบคีย์เวิร์ดชั่วคราวในโปรแกรมด้านบนและรันโปรแกรมใหม่อีกครั้ง
1 2 3 4 5 6 |
/ Library / Java / JavaVirtualMachines / jdk - 14.0.2.jdk / Contents / Home / bin / java - javaagent Company Name : Crunchify . com Number of Employee : 5 Zip code : 95112 Process finished with exit code 0 |
ดังที่คุณเห็นด้านบน ตอนนี้เราสามารถเห็นรหัสไปรษณีย์ 95112
โปรดแจ้งให้เราทราบหากคุณพบปัญหาใดๆ ในการจัดลำดับวัตถุและตัวแปรชั่วคราว