Java如何使用相同的名称/签名区分这些多个方法? [英] How does Java distinguish these multiple methods with the same name/signature?

查看:140
本文介绍了Java如何使用相同的名称/签名区分这些多个方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天正在追踪一个错误,我发现其中一个类中有一些奇怪的东西。我删除尽可能多的代码在这里发布:

I was tracking down a bug today, and I noticed something strange within one of our classes. I cut out as much code as possible to post here:

class A {
    static int obtainNumber() { return 42; }
    static int obtainNumber() { return 3; }
    static int obtainNumber() { return -1; }
    static {
        System.out.println(obtainNumber());
    }
}

此类有3个方法,名称完全相同,签名。起初我以为这是无效代码,但是eclipse会突出显示红色代码。它确实有效:

This class has 3 methods with the exact same name and signature. At first I thought this was invalid code, but then eclipse would have highlighted the code in red. It does work:

javac A.java && java A
42
Exception in thread "main" java.lang.NoSuchMethodError: main

所以我想也许Java会使用它看到的第一个。我重新测试了:

So I figured maybe Java will just use the first one it sees. I reordered to test:

class A {
    static int obtainNumber() { return 3; }
    static int obtainNumber() { return -1; }
    static int obtainNumber() { return 42; }
    static {
        System.out.println(obtainNumber());
    }
}

不,同样的结果:

javac A.java && java A
42
Exception in thread "main" java.lang.NoSuchMethodError: main

我想也许它使用的是42,因为它是最大的。为了测试这个,我拿了原始版本并更改了返回值:

I thought perhaps it uses the one with 42 because its the biggest. To test this, I took the original and changed the return values:

class A {
    static int obtainNumber() { return 0; }
    static int obtainNumber() { return 1; }
    static int obtainNumber() { return 2; }
    static {
        System.out.println(obtainNumber());
    }
}

它仍然知道使用第一个:

It still knows to use the first one:

javac A.java && java A
0
Exception in thread "main" java.lang.NoSuchMethodError: main

如果我再次对它们重新排序:

And if I reorder them again:

class A {
    static int obtainNumber() { return 1; }
    static int obtainNumber() { return 0; }
    static int obtainNumber() { return 2; }
    static {
        System.out.println(obtainNumber());
    }
}

同样的结果:

javac A.java && java A
0
Exception in thread "main" java.lang.NoSuchMethodError: main

我认为Java是一种基于文本的语言,我希望这种语言不可能。 Java如何跟踪哪种方法?

I thought Java was a text based language, which I'd expect makes this sort of thing impossible. How is Java tracking which method is which?

推荐答案

隐藏字符。源代码相当于

Hidden characters. The source code is equivalent to

static int obtainNumber(){return 42; }

static int获取 \ ufeff Number(){return 3; }

static int obtain\ufeffNumber() { return 3; }

static int获取 \ ufeff \ ufeff Number(){return -1; }

static int obtain\ufeff\ufeffNumber() { return -1; }

为了避免这种问题,我的源文件严格来说是US-ASCII。我想确定我看到的字符正是编译器看到的字符。

To avoid this kind of problems, my source files are strictly US-ASCII. I want to be certain that the characters I see are exactly the characters compiler sees.

这篇关于Java如何使用相同的名称/签名区分这些多个方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆