我已阅读Constructor Calls while serialization,Serialization Rules但无法找到会员变量的所有规则。
我有以下问题:
Student.java
class Person {
}
class Student extends Person implements Serialization {
List<Book> books;
Student(){}
Student(List<Book> books) {
this.books = books;
}
}
class Book implements Serialization {
Book() {}
String bookId;
Book(String bookId) {
this.bookId = bookId;
}
}
这里Student是可序列化的,Book类也是可序列化的,那么构造函数调用可序列化和反序列化的规则是什么。
答案 0 :(得分:0)
请参阅下面的答案:
Is required default constructor in Book class?
- &GT;是的,它是强制性的,否则序列化将失败。
How constructor calls while serialization and deserialization?
- &GT;序列化使用反射,并且必须使用默认构造函数来使用反射。
答案 1 :(得分:0)
实际上,您应该将a no-arg constructor
放在Person
的类中,Student
是java.io.InvalidClassException
的超类。
如果不是,反序列化时将抛出运行时{
"ProductId": 20,
"ProductName": "LCD",
"ProductDescription": "Sony",
"BuyingPrice": "15000 ",
"VendorId": 46,
"CategoryId": 25,
"CreatedBy": 0,
"UpdateBy": null,
"IsActive": true,
"IsDeleted": false,
"CreatedDate": "2018-03-14",
"UpdatedDate": null,
"WareHouseProducts": [
{
"WareHouseProductsId": 11,
"TotalQuantity": 15,
"AvailableQuantity": 8,
"ProductId": 20,
"WareHouseId": 4,
"CreatedBy": null,
"UpdateBy": null,
"IsActive": true,
"IsDeleted": false,
"CreatedDate": "2018-03-14",
"UpdatedDate": "2018-03-15",
"WarehouseName": "Warehouse 1"
}
]
}
。