// Page 226 import java.util.*; import java.io.*; public class MutablePeriod { // A period instance public final Period period; // period's start field, to which we shouldn't have access public final Date start; // period's end field, to which we shouldn't have access public final Date end; public MutablePeriod() { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); // Serialize a valid Period instance out.writeObject(new Period(new Date(), new Date())); /* * Append rogue "previous object refs" for internal * Date fields in Period. For details, see "Java * Object Serialization Specification," Section 6.4. */ byte[] ref = { 0x71, 0, 0x7e, 0, 5 }; // Ref #5 bos.write(ref); // The start field ref[4] = 4; // Ref # 4 bos.write(ref); // The end field // Deserialize Period and "stolen" Date references ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream(bos.toByteArray())); period = (Period) in.readObject(); start = (Date) in.readObject(); end = (Date) in.readObject(); } catch (Exception e) { throw new RuntimeException(e.toString()); } } public static void main(String[] args) { MutablePeriod mp = new MutablePeriod(); Period p = mp.period; Date pEnd = mp.end; // Let's turn back the clock pEnd.setYear(78); System.out.println(p); // Bring back the 60's! pEnd.setYear(69); System.out.println(p); } }


微信网友_7565568760549376
6月22日
暂无贡献等级
湮灭圣者 LV1
2024年1月20日
15575564465 LV1
2023年9月14日
朱俪的邮件及存储 LV8
2023年3月22日
绝对零度 LV4
2022年8月2日
你脚上银铃响了 LV1
2021年10月27日
大隐隐于市 LV2
2021年7月2日
916403064 LV2
2021年6月13日
smile soft
2021年4月28日
暂无贡献等级
有理想的中年人 LV1
2021年3月16日