关于Core Java中一句话的理解

本文探讨了在面向对象编程中,如何通过正确使用封装原则避免对象状态的意外修改,并介绍了使用clone方法复制可变对象以保护原始数据的重要性。

最近在翻翻以前看过的书,看到Core Java里的一句话:As a rule of thumb, always use clone whenever you need to return a copy of a mutable data field.大意是说:一般来讲,对于返回变化的变量,最好用clone()方法。结合上下文来理解这句话,其言外之意,就是如果不经处理的返回实例的对象变量或者属性,是一种破坏封装的行为。

还是先来看下最直接的破坏封装的编程行为:

class Person{ public int age; //public修饰变量,导致封装被破坏 public Person(int age){ this.age = age; } public String toString(){ return "The person's age is "+ age; } } public class NotGood { public static void main(String args[]){ Person p = new Person(24); p.age = 40; //能够直接通过变量访问变量,而不是通过方法 System.out.println(p); } }

上面的代码,明显的反应了封装的一个基本思想:通过类自身的方法去访问或者改变变量,而不应该直接通过类的实例化对象直接去操作变量。

回到我们开始是提起的那句话,那为什么不能直接返回可变的变量呢?看代码和输出:

import java.util.*; import java.text.*; class Person{ private int age; private Date birthday; public Person(int age, int year, int month, int day){ this.age = age; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); this.birthday = calendar.getTime(); } public int getAge(){ return age; } public Date getBirth(){ return birthday; } public String toString(){ return "The person's age is "+ age +", and born at "+ new SimpleDateFormat("yyyy-MM-dd").format(birthday) +"."; } } public class NotGood { public static void main(String args[]){ Person p = new Person(24, 1984, 12, 1); int i = p.getAge(); i++; System.out.println(p); Date d = p.getBirth(); double tenYearsInMilliSeconds = 10 * 365.25 * 24 * 60 * 60 * 1000; d.setTime(d.getTime() - (long) tenYearsInMilliSeconds); System.out.println(p); } }

结果:

The person's age is 24, and born at 1984-12-01. The person's age is 24, and born at 1974-12-01.

可以看出来,对于基本类型的变量输出没有影响,但是如果变量是个对象的话,修改之后,就会对原始的对象变量的内容。这就Core Java这句话的意思。按照它的建议,应该处理下,最直接的办法就是利用clone()方法。看代码和输出:

import java.util.*; import java.text.*; class Person{ private int age; private Date birthday; public Person(int age, int year, int month, int day){ this.age = age; GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day); this.birthday = calendar.getTime(); } public int getAge(){ return age; } public Date getBirth(){ return (Date)birthday.clone(); //在这里进行修改 } public String toString(){ return "The person's age is "+ age +", and born at "+ new SimpleDateFormat("yyyy-MM-dd").format(birthday) +"."; } } public class NotGood { public static void main(String args[]){ Person p = new Person(24, 1984, 12, 1); System.out.println(p); Date d = p.getBirth(); double tenYearsInMilliSeconds = 10 * 365.25 * 24 * 60 * 60 * 1000; d.setTime(d.getTime() - (long) tenYearsInMilliSeconds); System.out.println(p); } }

输出:

The person's age is 24, and born at 1984-12-01. The person's age is 24, and born at 1984-12-01.

MATLAB主动噪声和振动控制算法——对较大的次级路径变化具有鲁棒性内容概要:本文主要介绍了一种在MATLAB环境下实现的主动噪声和振动控制算法,该算法针对较大的次级路径变化具有较强的鲁棒性。文中详细阐述了算法的设计原理与实现方法,重点解决了传统控制系统中因次级路径动态变化导致性能下降的问题。通过引入自适应机制和鲁棒控制策略,提升了系统在复杂环境下的稳定性和控制精度,适用于需要高精度噪声与振动抑制的实际工程场景。此外,文档还列举了多个MATLAB仿真实例及相关科研技术服务内容,涵盖信号处理、智能优化、机器学习等多个交叉领域。; 适合人群:具备一定MATLAB编程基础和控制系统理论知识的科研人员及工程技术人员,尤其适合从事噪声与振动控制、信号处理、自动化等相关领域的研究生和工程师。; 使用场景及目标:①应用于汽车、航空航天、精密仪器等对噪声和振动敏感的工业领域;②用于提升现有主动控制系统对参数变化的适应能力;③为相关科研项目提供算法验证与仿真平台支持; 阅读建议:建议读者结合提供的MATLAB代码进行仿真实验,深入理解算法在不同次级路径条件下的响应特性,并可通过调整控制参数进一步探究其鲁棒性边界。同时可参考文档中列出的相关技术案例拓展应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值