简体   繁体   English

我对理解一些 Java 代码有疑问

[英]I Have a problem with understanding some Java code

The Code:编码:

package com.keyoti.rapidSpell;

import java.util.Comparator;

// Referenced classes of package com.keyoti.rapidSpell:
//            RapidSpellChecker

class RapidSpellChecker$CompareL
    implements Comparator
{

    public int compare(Object a, Object b)
    {
        return (int)(100D * (suggestionScore2b(topWord, (String)b) - suggestionScore2b(topWord, (String)a)));
    }

    public void with(String w)
    {
        topWord = w;
    }

    private String topWord;

    RapidSpellChecker$CompareL()
    {
    }
}

This is the one the many classes in the application.这是应用程序中的众多类之一。

What does the $ sign in class RapidSpellChecker$CompareL implements Comparator signify?Is it simply the class name or has some significance? class RapidSpellChecker$CompareL implements Comparator中的$符号是什么意思?它只是 class 名称还是有什么意义?

I suspect this is decompiled code.我怀疑这是反编译的代码。 (See at the bottom for more information.) The $ shows that it's a nested class within RapidSpellChecker . (有关更多信息,请参阅底部。) $ 表示它是 RapidSpellChecker 内的嵌套RapidSpellChecker So the code would originally have looked something like this:所以代码最初看起来像这样:

public class RapidSpellChecker
{
    // Other code withing RapidSpellChecker

    static class CompareL implements Comparator
    {
        // Code for compare, with etc
    }
}

I've shown this as a static nested class, because the code you've shown doesn't have any implicit reference to an instance of RapidSpellChecker .我已将其显示为嵌套 class 的static ,因为您显示的代码没有对RapidSpellChecker实例的任何隐式引用。 If it did, the original code would have been like this:如果是这样,原始代码将是这样的:

public class RapidSpellChecker
{
    // Other code withing RapidSpellChecker

    class CompareL implements Comparator
    {
        // Code for compare, with etc
    }
}

In this case it's an inner class .在这种情况下,它是一个内部 class

See the Java tutorial on nested classes for more information.有关更多信息,请参阅有关嵌套类的 Java 教程


EDIT: I originally thought this was invalid code;编辑:我最初认为这是无效代码; that you couldn't use $ in an identifier in Java to start with.你不能在 Java 的标识符中使用 $ 开始。 It turns out I'm wrong.事实证明我错了。 From the Java Language Specification, section 3.8 :来自Java 语言规范,第 3.8 节

The $ character should be used only in mechanically generated source code or, rarely, to access preexisting names on legacy systems. $ 字符应仅用于机械生成的源代码中,或者很少用于访问遗留系统上预先存在的名称。

So it's valid, just discouraged.所以它是有效的,只是气馁。

That's a nested class.那是一个嵌套的 class。 When the Java compiler compiles a class with nested classes, it separates all of them in different.class files.当 Java 编译器编译带有嵌套类的 class 时,它将所有这些类分隔在不同的.class 文件中。

class A {
  class B {
  }
}

gives A.class and A$B.class给出 A.class 和 A$B.class

You can use $ in a variable name if you want.如果需要,可以在变量名中使用$ In a variable name it has no special significance.在变量名中它没有特殊意义。

$ is also typically used to indicate inner classes when you compile using javac $也通常用于在使用javac编译时指示内部类

If you compile如果你编译

class A {
    class B {
    }
}

You'll see A.class created and B.class.您将看到创建了 A.class 和 B.class。

For fun and amusement, you could create confusing looking "JQuery"-esque code in Java (you need the static import to use the $ static method).为了有趣和娱乐,您可以在 Java 中创建看起来令人困惑的“JQuery”式代码(您需要 static 导入才能使用$ ZA81259CEF8E959C624DF1D456E5D3297 方法)。 See the example below:请参见下面的示例:

import static thisPackage.*;

public class $ {
    public static $ $(String s) { return new $(s); }
    public $ fadeIn(int fade) { return this; }
    public $ slideUp(int slide) { return this; }
    public $ delay(int ms) { return this; }
    public $(String s) { }

    public static void main(String[] args) {
       $("#foo").slideUp(300).delay(800).fadeIn(400);
    }
} 

Implementing this with a DOM library underneath would be a fun project!用下面的 DOM 库来实现这个将是一个有趣的项目!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM