坚持使用Overrider注解,可以预防我们并没有重载的情况出现。

除非使用者绝对安全,不然使用保护性拷贝,可以使程序安全。

public class Period {//没有拷贝安全
private final Date start;
private final Date end; public Period(Date start, Date end) {
if (start.compareTo(end) > 0) {
throw new IllegalArgumentException("开始时间比结束时间晚");
} this.start = start;
this.end = end; } public Date getStart() {
return this.start;
} public Date getEnd() {
return this.end;
}
} public class PeriodProtect {
private final Date start;
private final Date end; public PeriodProtect(Date start, Date end) {//使用了保护性拷贝,保证安全
this.start = new Date(start.getTime());
this.end = new Date(end.getTime()); if (this.start.compareTo(this.end) > 0) {//先拷贝后检查,是因为防止一个叫TOCTOU的一种漏洞攻击。
throw new IllegalArgumentException("开始时间比结束时间晚");
}
} public Date getStart() {
return this.start;
} public Date getEnd() {
return this.end;
}
} public class Test { public static void main(String[] args) {
Date start = new Date();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date end = new Date(); Period p = new Period(start, end);
PeriodProtect pro = new PeriodProtect(start, end); end.setYear(88); Date d = p.getEnd();
Date dp = pro.getEnd(); System.out.println(d);//输出 Sat Aug 20 21:04:56 CDT 1988 ,没有保护性拷贝,会根据修改end的参数被修改period里面的值,比较不安全。
System.out.println(dp);//会正确输出Sat Aug 20 21:04:56 CDT 2018,使用了拷贝安全之后就能够保证不会被篡改。
}
}

重载(Overload)和覆盖(Overrider)

public class CollectionClassfier {//测试重载Overloded,在Overloading的时候是编译时选择,所以会是选择collection
public static String classfier(List<?> list) {
return "list";
} public static String classfier(Set<?> set) {
return "set";
} public static String classfier(Collection<?> col) {
return "collection";
} public static void main(String[] args) {
Collection<?>[] col = { new ArrayList<String>(), new HashMap<String, String>().values(),
new HashSet<String>() }; for (Collection<?> c : col) {
System.out.println(classfier(c));
}
/* 输出
collection
collection
collection
*/
} } //////////////////////////////////////////测试覆盖////////////////////////////////// public class Wine {
String name() {
return "Wine";
}
} public class BWine extends Wine{
@Override
String name(){
return "BWine";
}
} public class CWine extends BWine {
@Override
String name() {
return "CWine";
}
} public class TestForOverride {//测试覆盖 public static void main(String[] args) {
Wine[] wine = {new Wine(),new BWine(),new CWine()};
for(Wine w:wine){
System.out.println(w.name());
}
}
}
/*输出
* Wine
BWine
CWine * */

会发现在第一个例子里面,使用的是重载,而重载是在编译时就选择好方法,在for循环中使用了Collection<?> c,所以之后执行的方法也是在选择重载的Collection,

而在覆盖中Wine w:wine,即使我们传入的是Wine,因为覆盖是在程序运行时确定,所以,可以正确识别我们需要执行的方法。

一个关于List 重载的小陷阱

public class SetList {

    public static void main(String[] args) {
Set<Integer> set = new TreeSet<Integer>();
List<Integer> list = new ArrayList<Integer>();
for (int i = -3; i < 3; i++) {
set.add(i);
list.add(i);
}
for (int i = 0; i < 3; i++) {
set.remove(i);//这里是renmove object
list.remove(i);//这里是remove index
}
System.out.println(list + " " + set); }
/*
*
* 输出
* [-2, 0, 2] [-3, -2, -1]
*
*
* */
}

我们想要在数组[-3,-2,-1,0,1,2]中剔除[0,1,2]这三个数值,然而set成功了list却失败了。原因是因为,在java引入了自动封装后,我们可以在set里面直接传入 int类型数值,java会自动封装成Integer类型,而list却不会,因为list的remove有两种,一种是remove(object o),一种是 remove(int index),我们传入的是int,他会执行的是remove(int index),第一次执行remove(0),剔除-3 变成[-2,-1,0,1,2],第二次remove(1),变成[-2,0,1,2],第三次remove(2)变成[-2, 0, 2] ,如果想要remove对应对象,要把int变成Integer。

最新文章

  1. QT 删除文件指定目录
  2. Spring整合log4j
  3. Pair Project:电梯控制程序
  4. java 之前的安全的类回顾,以及以后需要线程安全时使用哪些类
  5. Code笔记之:CSS块级元素、内联元素概念
  6. [ASP.NET] Dictionary 和 Hashtable 区别
  7. Objective-C(面向对象的三大特性)
  8. Jquery select 选中项中自定义的值
  9. Linux的服务器初始优化脚本。
  10. [jobdu]最小的K个数
  11. IE-一根网线二台电脑上网
  12. Nmon 性能:分析 AIX 和 Linux 性能的免费工具
  13. Swift2.0 UITextView 和 UITextFile 的使用
  14. 初识Hibernate之关联映射(一)
  15. java 读取数据库中文信息,为何在jsp页面中出现乱码
  16. html下载excel模板
  17. 《linux就该这么学》第十六节课:第16,17章,Squid服务和iscsi网络存储
  18. bui前端框架+yii整理
  19. EditPlus配置GCC
  20. 5G频谱到底有多值钱?

热门文章

  1. 圆的变化(自定义动画,及自定义UI)
  2. 【洛谷 P1216】【IOI1994】【USACO1.5】数字三角形 Number Triangles
  3. dfs的一个小实现(啊哈算法的例题)
  4. 『MXNet』第二弹_Gluon构建模型
  5. 迭代FFT
  6. PAT 1005 Spell It Right
  7. 牛客网第一场E题 Removal
  8. Scanner类完成用户键盘录入
  9. samba服务断开某个用户 samba客户端断开自动登录
  10. javascript 多个onclick function 取对应值