类文件名中的 $1 是什么?

发布于 2024-07-26 04:46:43 字数 2808 浏览 5 评论 0 原文

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir
 Volume in drive C has no label.
 Volume Serial Number is 2041-64E7

 Directory of C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet

2009-07-02  23:54              .
2009-07-02  23:54              ..
2004-09-06  14:57               582 WelcomeApplet.html
2004-09-06  15:04             1,402 WelcomeApplet.java
               2 File(s)          1,984 bytes
               2 Dir(s)   2,557,210,624 bytes free

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>javac WelcomeApplet.java

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir
 Volume in drive C has no label.
 Volume Serial Number is 2041-64E7

 Directory of C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet

2009-07-02  23:54              .
2009-07-02  23:54              ..
2009-07-02  23:54               975 WelcomeApplet$1.class
2009-07-02  23:54             1,379 WelcomeApplet.class
2004-09-06  14:57               582 WelcomeApplet.html
2004-09-06  15:04             1,402 WelcomeApplet.java
               4 File(s)          4,338 bytes
               2 Dir(s)   2,557,202,432 bytes free

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>

以下是该 Java 文件的内容:

/**
   @version 1.21 2002-06-19
   @author Cay Horstmann
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class WelcomeApplet extends JApplet
{
   public void init()
   {
      setLayout(new BorderLayout());

      JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);
      label.setFont(new Font("Serif", Font.BOLD, 18));
      add(label, BorderLayout.CENTER);

      JPanel panel = new JPanel();

      JButton cayButton = new JButton("Cay Horstmann");
      cayButton.addActionListener(makeURLActionListener(
         "http://www.horstmann.com"));
      panel.add(cayButton);

      JButton garyButton = new JButton("Gary Cornell");
      garyButton.addActionListener(makeURLActionListener(
         "mailto:[email protected]"));
      panel.add(garyButton);

      add(panel, BorderLayout.SOUTH);
   }

   private ActionListener makeURLActionListener(final String u)
   {
      return new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               try
               {
                  getAppletContext().showDocument(new URL(u));
               }
               catch(MalformedURLException e) 
               { 
                  e.printStackTrace(); 
               }
            }
         };
   }
}
C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir
 Volume in drive C has no label.
 Volume Serial Number is 2041-64E7

 Directory of C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet

2009-07-02  23:54              .
2009-07-02  23:54              ..
2004-09-06  14:57               582 WelcomeApplet.html
2004-09-06  15:04             1,402 WelcomeApplet.java
               2 File(s)          1,984 bytes
               2 Dir(s)   2,557,210,624 bytes free

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>javac WelcomeApplet.java

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>dir
 Volume in drive C has no label.
 Volume Serial Number is 2041-64E7

 Directory of C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet

2009-07-02  23:54              .
2009-07-02  23:54              ..
2009-07-02  23:54               975 WelcomeApplet$1.class
2009-07-02  23:54             1,379 WelcomeApplet.class
2004-09-06  14:57               582 WelcomeApplet.html
2004-09-06  15:04             1,402 WelcomeApplet.java
               4 File(s)          4,338 bytes
               2 Dir(s)   2,557,202,432 bytes free

C:\Program Files\Java\jdk1.6.0_05\CoreJava\v1\v1ch2\WelcomeApplet>

Here is the content of that Java file:

/**
   @version 1.21 2002-06-19
   @author Cay Horstmann
*/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class WelcomeApplet extends JApplet
{
   public void init()
   {
      setLayout(new BorderLayout());

      JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER);
      label.setFont(new Font("Serif", Font.BOLD, 18));
      add(label, BorderLayout.CENTER);

      JPanel panel = new JPanel();

      JButton cayButton = new JButton("Cay Horstmann");
      cayButton.addActionListener(makeURLActionListener(
         "http://www.horstmann.com"));
      panel.add(cayButton);

      JButton garyButton = new JButton("Gary Cornell");
      garyButton.addActionListener(makeURLActionListener(
         "mailto:[email protected]"));
      panel.add(garyButton);

      add(panel, BorderLayout.SOUTH);
   }

   private ActionListener makeURLActionListener(final String u)
   {
      return new
         ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               try
               {
                  getAppletContext().showDocument(new URL(u));
               }
               catch(MalformedURLException e) 
               { 
                  e.printStackTrace(); 
               }
            }
         };
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

久隐师 2024-08-02 04:46:43

这些是保存匿名内部类.class文件。

在您的示例中,WelcomeApplet.java 包含一个顶级类(称为 WelcomeApplet)和一个匿名内部类,该内部类将存储在 WelcomeApplet$1.class.

请注意,保存匿名内部类的文件的确切名称并未标准化,并且可能会有所不同。 但实际上,除了这里描述的方案之外,我还没有看到任何其他方案。

enum 的值特定主体 也是匿名内部类

枚举常量的可选类主体隐式定义匿名类声明 (§15.9.5) 扩展直接封闭的枚举类型。

Those are the .class files that hold the anonymous inner classes.

In your example WelcomeApplet.java contains a top-level class (called WelcomeApplet) and an anonymous inner class, which will be stored in WelcomeApplet$1.class.

Note that the exact name of the files holding anonymous inner classes is not standardized and might vary. But in practice I've yet to see any other scheme than the one described here.

Value-specific bodies for an enum are also anonymous inner classes:

The optional class body of an enum constant implicitly defines an anonymous class declaration (§15.9.5) that extends the immediately enclosing enum type.

玩套路吗 2024-08-02 04:46:43

$1 是您在 WelcomeApplet.java 文件中定义的匿名内部类。

例如,编译

public class Run {
    public static void main(String[] args) {
        System.out.println(new Object() {
            public String toString() {
                return "77";
            }
        });
    }
    private class innerNamed {
    }
}

将生成 Run.classRun$1.classRun$innerNamed.class

The $1 are anonymous inner classes you defined in your WelcomeApplet.java file.

e.g. compiling

public class Run {
    public static void main(String[] args) {
        System.out.println(new Object() {
            public String toString() {
                return "77";
            }
        });
    }
    private class innerNamed {
    }
}

would result in Run.class, Run$1.class and Run$innerNamed.class being generated

再见回来 2024-08-02 04:46:43

这些是由 java 编译器从 WelcomeApplet.java 文件中的内部和静态嵌套类生成的。

另请参阅此类似问题与解答

These are generated from the inner and static nested classes in the WelcomeApplet.java file by the java compiler.

See also this similar question and answer.

把人绕傻吧 2024-08-02 04:46:43

它来自这“行”代码:

return new
    ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            try
            {
                getAppletContext().showDocument(new URL(u));
            }
            catch(MalformedURLException e) 
            { 
                e.printStackTrace(); 
            }
        }
    };

每次调用该方法时,您声明 ActionListener 的方式都会创建匿名内部类的实例。

即使没有调用该方法,上面的行仍然会被编译为匿名内部类。

It is from this 'line' of code:

return new
    ActionListener()
    {
        public void actionPerformed(ActionEvent event)
        {
            try
            {
                getAppletContext().showDocument(new URL(u));
            }
            catch(MalformedURLException e) 
            { 
                e.printStackTrace(); 
            }
        }
    };

The way you are declaring the ActionListener you are making an instance of the anonymous inner class each time that method is called.

Even if the method is not called, the above line still gets compiled into an anonymous inner class no matter what.

独﹏钓一江月 2024-08-02 04:46:43

WelcomeApplet$1.class 文件是为 WelcomeApplet.java 源中的匿名类生成的(该匿名类是在方法调用 makeURLActionListener 中通过调用 new new ActionListener() {... 为了更清楚地

解释,每当您有一个具体命名类的实例化时,匿名类都会在编译时生成,该具体命名类会内联覆盖具体类(或接口)的部分或全部行为,如下所示:

class HelloInternalClass {
  public static final void main(String[] args) {
    // print in another thread
    new Thread(new Runnable() {
      public void run() {
        System.out.println("Printed from another thread");
      }
    }).start();
  }
}

在上面的示例代码中,javac 编译器将生成 2 个类文件,就像您的示例中一样:HelloInternalClass.classHelloInternalClass$1.class

此实例中的匿名类将是 Runnable 的子类,并将被编译为 HelloInternalClass$1.class 。 顺便说一句,如果您从上面示例中的可运行实例中询问类名(通过调用 getClass().getName()),您会发现它会将自己视为“HelloInternalClass$1 ”。

The WelcomeApplet$1.class file is generated for an anonymous class in the WelcomeApplet.java source (the anonymous class is generated in the method call makeURLActionListener by calling new new ActionListener() {...})

To explain more clearly, the anonymous classes are generated at compile time any time you have an instantiation of a concrete named class that overrides some or all of the behavior of the concrete class (or interface) inline like this:

class HelloInternalClass {
  public static final void main(String[] args) {
    // print in another thread
    new Thread(new Runnable() {
      public void run() {
        System.out.println("Printed from another thread");
      }
    }).start();
  }
}

In the above sample code, the javac compiler would generate 2 class files just like in your example: HelloInternalClass.class and HelloInternalClass$1.class.

The anonymous class in this instance would be a subclass of Runnable and would be compiled into HelloInternalClass$1.class. Incidentally, if you would ask a class name from the runnable instance in the above sample (by calling getClass().getName()) you would find that it thinks of itself as "HelloInternalClass$1".

薄荷→糖丶微凉 2024-08-02 04:46:43

创建:

public class A {
    public static void main(String[] args) {
        X x=new X();
        X x2=new X(){   
        };
        Class<? extends Object>c2=x2.getClass();
        Class<? extends Object>s2=x2.getClass().getSuperclass();

        boolean b=false;
    }
    private static class X{     
    }
}

从代码中很难看出(new X{}() 会比 new X(){} 更好),但是 x2< /code> 是 A$X 子类的实例。 该子类是 A$1

Create:

public class A {
    public static void main(String[] args) {
        X x=new X();
        X x2=new X(){   
        };
        Class<? extends Object>c2=x2.getClass();
        Class<? extends Object>s2=x2.getClass().getSuperclass();

        boolean b=false;
    }
    private static class X{     
    }
}

It is hard to see from the code (new X{}() would have been better than new X(){}), but x2 is an instance of a subclass of A$X. This subclass is A$1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文