- 浏览: 728443 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (419)
- 杂软粉墨 (2)
- 创意灵感 (3)
- 经验记录 (137)
- 开源轨迹 (2)
- sip-communicator (2)
- 闲侃杂谈 (8)
- 问题交流 (24)
- 概念模式 (32)
- 难点备案 (5)
- JwChat (1)
- 中国象棋 (1)
- 教育探索 (6)
- 英语研究 (58)
- 星际争霸 (1)
- 电信知识 (1)
- 软件架构 (3)
- 哲学探索 (26)
- 算法灵魂 (8)
- 近视探索 (6)
- 数学数学 (3)
- 牛角钻尖 (23)
- 至强文言 (3)
- 数据结构 (1)
- 宇宙物理 (2)
- 网络架构 (3)
- 游戏领域 (4)
- 图形处理 (2)
- 修炼之路 (8)
- 读书天地 (20)
- 编解乱码 (2)
- 概念探索 (8)
- 格物致知 (1)
- 其它语言 (1)
- 测试领域 (3)
- 文化风流 (1)
- JQuery (1)
- 網頁領域 (1)
- Unix/Linux (1)
- Inside JVM (1)
- 异常分析 (1)
最新评论
-
suyujie:
引用
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
suyujie:
HTML <a> 标签灰显禁用 -
iamzhoug37:
您能说一下"局部变量不受文本顺序限制" 是 ...
声明前为什么能赋值却不能输出,都是使用
Effective Java 2rd ITEM 41: USE OVERLOADING JUDICIOUSLY
审慎地使用重载,看下面的例子,猜一下结果会是什么:
import java.util.ArrayList; import java.util.List; import java.util.Set; import java.util.TreeSet; 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); list.remove(i); } System.out.println(set + " " + list); } }
安全、保守的策略是不要设计两个具有相同参数的重载函数,除非参数类型差别很大,Two types are radically different if it is clearly impossible to cast an instance of either type to the other.
上例的结果是[-3, -2, -1] [-2, 0, 2],原因解释如下:
Here’s what’s happening: The call to set.remove(i) selects the overloading
remove(E), where E is the element type of the set (Integer), and autoboxes i
from int to Integer. This is the behavior you’d expect, so the program ends up
removing the positive values from the set. The call to list.remove(i), on the
other hand, selects the overloading remove(int i), which removes the element at
the specified position from a list. If you start with the list [-3, -2, -1, 0, 1, 2]
and remove the zeroth element, then the first, and then the second, you’re left with
[-2, 0, 2], and the mystery is solved. To fix the problem, cast list.remove’s
argument to Integer, forcing the correct overloading to be selected. Alternatively,
you could invoke Integer.valueOf on i and pass the result to list.remove.
Either way, the program prints [-3, -2, -1] [-3, -2, -1], as expected:
for (int i = 0; i < 3; i++) { set.remove(i); list.remove((Integer) i); // or remove(Integer.valueOf(i)) }
主要是说的Set的remove选择了remove(E)重载版本(只有这一个吧),而List选择了remove(i)
发表评论
-
关于方法访问控制符protected
2012-11-29 10:38 1200http://bbs.csdn.net/topics/3902 ... -
一个基本问题关于引用的
2012-05-15 10:20 1078问: int a = 1; Integer b = new ... -
我對面向對象和過程的理解。
2012-05-02 08:30 1011我的一些理解。 面向过程,是对客观现象的描述,感觉是有一个上 ... -
stack and heap
2012-01-13 23:17 1011我觉得是根据应用方式 和本身特性 才将内存分区的,目的是提 ... -
program experience conclusion
2011-07-11 15:35 10141. check parameters for validit ... -
PreparedStatement's possible designated parameter
2011-04-29 13:45 939though it's nearly impossible t ... -
clean Log4j
2011-04-12 11:19 1026import org.apache.log4j.BasicCo ... -
about abstract class
2011-04-02 10:34 809yes, we do know abstract class ... -
cvs operations on linux
2011-03-25 09:40 959http://www.linuxhowtos.org/Syst ... -
regex to exchange two parts
2011-03-24 17:09 1010public class Test { public ... -
About the database locking
2011-03-09 11:02 903http://en.wikipedia.org/wiki/Lo ... -
how to send soap message in java
2011-03-08 10:29 1854import java.io.BufferedReader; ... -
About ShutDownDemo
2011-03-07 15:02 922public class ShutdownDemo { p ... -
How do you know if an explicit object casting is needed
2011-02-24 16:33 1121通俗来讲,不可能将一只是猫的动物强转为狗 再说Graphic ... -
有关MimeUtility
2011-02-24 13:11 3298import java.io.UnsupportedEncod ... -
C#连接sql server 2008的一件2事
2011-02-24 09:01 2112once upon a time, i came upon o ... -
Shadowing, Overriding, Hiding and Obscuring
2011-02-22 15:15 1098当子类属性与父类属性重叠时 这种叫法上是shadowi ... -
JAXP usage
2011-02-16 16:07 1052import java.io.ByteArrayInputSt ... -
运行一个类,如果classpath中路径带空格就加双引号
2011-02-11 11:25 2762注意是这样加: java -cp .;"d:\my ... -
关于ClassPath中的current directory
2011-01-28 16:40 1115Given: 1. package com.company. ...
相关推荐
Item 52: Use overloading judiciously Item 53: Use varargs judiciously Item 54: Return empty collections or arrays, not nulls Item 55: Return optionals judiciously Item 56: Write doc comments for all ...
Overloading&Overriding两者的区别
operator-overloading-js, JS简单运算符重载库 目录Operator-Overloading-JS安装工具node MODULE ( NPM )浏览器( Bower )Sneak示例重载运算符设计考虑/非常重要/必须阅读。Definig使用运算符重载的上下文
函数式编程入门论文 讲解常见的“函数式”特性
operator overloading and copy constructors 英文原版。英国大学英文原本教材
Overloading
软件设计课件:Lecture 07 C Operator Overloading.ppt
软件设计课件:Lecture 07 C++ Operator Overloading.ppt
VB.NET - Framework - Scoping, Overloading, Overriding
VC运算符重载开发经典资料Development of the classic VC data operator overloading
Java 实例 - 重载(overloading)方法中使用 Varargs技术包含源代码和详细教程.zip
关于C++操作符重载的一个文档。有了它,关于操作符重载的东西你就不用再找资料拉~
MethodOverloading
Python是一种解释型的、面向对象的、带有动态语义的高级程序设计语言。它是由荷兰人吉多·罗萨姆于1989年发布的,第一个公开发行版发行于1991年。Python注重解决问题的方法,而不是语法和结构。...
Use operator overloading Extend classes with inheritance Use polymorphism and derived classes Employ object-oriented analysis and design Table of Contents Part I: Beginning C++ HOUR 1: Writing Your ...
北京交通大学C++教学课件,内部资源,内容十分好,适合有一部分C功底的初学者!
C++: 函数重载
statements via operator and function overloading. Currently the forward mode of automatic differentiation is supported via the fmad class. For a single directional derivative objects of the fmad class...
Overloading-Method