The choice of which overloading to invoke is made at compile time.

// Broken! - What does this program print?

public class CollectionClassifier {

public static String classify(Set<?> s) {

return "Set";

}

public static String classify(List<?> lst) {

return "List";

}

public static String classify(Collection<?> c) {

return "Unknown Collection";

}

public static void main(String[] args) {

Collection<?>[] collections = {

new HashSet<String>(),

new ArrayList<BigInteger>(),

new HashMap<String, String>().values()

};

for (Collection<?> c : collections)

System.out.println(classify(c));

// the result will prints three times "Unknown Collection"

}

}

Selection among overloaded methods is static, while selection among overridden methods is dynamic.

The compile-time type of an object has no effect on which method is executed when an overridden method is invoked; the "most specific" overriding method always gets executed.

class Wine {

String name() { return "wine"; }

}

class SparklingWine extends Wine {

@Override String name() { return "sparkling wine"; }

}

class Champagne extends SparklingWine {

@Override String name() { return "champagne"; }

}

public class Overriding {

public static void main(String[] args) {

Wine[] wines = {

new Wine(), new SparklingWine(), new Champagne()

};

for (Wine wine : wines)

System.out.println(wine.name());

// This will print "wine" "sparking wine" "Champagne"

}

}

Principle

  1. A safe, conservative policy is never to export two overloadings with the same number of parameters.

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);

// This will prints [-3, -2, -1] [-2, 0, 2]

}

}

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.

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.

2. The standard way to ensure behavior of the types of the same super class or interface as the parameter of a method is to have the more specific overloading forward to the more general.

public boolean contentEquals(StringBuffer sb) {

return contentEquals((CharSequence) sb);

}

Summary

You should generally refrain from overloading methods with multiple signatures that have the same number of parameters. You can name the method with same prefix rather than overloading the write method. Such as, these variants of ObjectOutputStream have signatures like writeBoolean(boolean), writeInt(int), and writeLong(long).

In some cases, especially where constructors are involved, it may be impossible to follow this advice. In that case, you should at least avoid situations where the same set of parameters can be passed to different overloadings by the addition of casts. In this case you have the option of exporting static factories instead of constructors (Item 1).

If such a situation cannot be avoided, for example, because you are retrofitting an existing class to implement a new interface, you should ensure that all overloadings behave identically when passed the same parameters.

最新文章

  1. Redhat、CentOS添加静态路由的标准方法
  2. ASP.NET MVC Json()处理大数据异常解决方法,字符串的长度超过了为 maxJsonLength
  3. Swing 顶层容器
  4. [Java] Java 获取数据库所有表基本信息和表中的所有列基本信息代码
  5. Newtonsoft.Json版本冲突时参考解决方案
  6. nyoj 199 无线网络覆盖
  7. android实现图片识别的几种方法
  8. JDFS:一款分布式文件管理实用程序第二篇(更新升级、解决一些bug)
  9. python 密码学编程
  10. 从壹开始前后端分离 [ vue + .netcore 补充教程 ] 三十║ Nuxt实战:动态路由+同构
  11. MyBatis-Plus初步使用
  12. 安装配置JDK和Eclipse的步骤
  13. 浅谈css3长度单位rem,以及移动端布局技巧
  14. Spring的第三天AOP之xml版
  15. 每日英语:Don&#39;t Call Us Bossy
  16. angularjs路由传递参数
  17. Java理论学时第六节。课后作业。
  18. Mariadb&#160;MySQL、Mariadb中GROUP_CONCAT函数使用介绍
  19. ASP.NET MVC学习笔记-----Filter(2)
  20. Nginx面试

热门文章

  1. Sprint 3计划
  2. 【EF 译文系列】重试执行策略的局限性(EF 版本至少为 6)
  3. 改变Visual Studio 2012的皮肤
  4. 使用VS GDB扩充套件在VS上远端侦错Linux上的C/C++程序
  5. 重新想象 Windows 8.1 Store Apps (82) - 绑定: DataContextChanged, TargetNullValue, FallbackValue, UpdateSourceTrigger
  6. kfreebsd不适用于实际环境
  7. ACdream 1214---矩阵连乘
  8. A除以B问题
  9. javascript数组浅谈2
  10. js验证真实姓名与身份证号,手机号