在java中,一个方法的签名包括方法标志符(identifier)和方法的参数列表(arguments list)。《Thinking in java》中有一个有趣的例子:
可以看到,在23行向b发送消息时,采用的是类Homer中定义的方法doh(float)回应,而没有采用Hide本身的。
- class Homer{
- char doh(char c) {
- System.out.println("doh(char)");
- return 'd';
- }
- float doh(float f) {
- System.out.println("doh(float)");
- return 1.0f;
- }
- }
- class Milhouse {}
- class Bart extends Homer {
- void doh(Milhouse m) {
- System.out.println("doh(Milhouse)");
- }
- }
- public class Hide {
- public static void main(String[] args) {
- Bart b = new Bart();
- b.doh(1);
- b.doh('x');
- b.doh(1.0f);
- b.doh(new Milhouse());
- }
- } /* Output:
- doh(float)
- doh(char)
- doh(float)
- doh(Milhouse)
- *///:~