Java 类:匿名 vs 嵌套 vs 私有 [英] Java Classes: Anonymous vs Nested vs Private

查看:33
本文介绍了Java 类:匿名 vs 嵌套 vs 私有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能解释一下Java中匿名类、嵌套类和私有类的区别?我想知道与每个相关的运行时成本以及每个的编译器方法,因此我可以掌握哪个最适合用于例如性能(编译器优化的潜力)、内存使用以及与其他 Java 编码人员的普遍可接受性.

Can someone explain the difference between anonymous classes, nested classes and private classes in Java? I'd like to know the runtime costs associated with each and the compiler approach to each, so I can get a grip on which is best to use for e.g. performance (potential for compiler optimisations), memory usage, and general acceptability with other Java coders.

匿名类是指那些以奇怪的 Java 方式在函数中内联声明的类.这在 Swing 代码的示例中经常看到,在这些示例中,您编写了一个与其声明内联的按钮处理程序.我真的不喜欢匿名类,因为你会得到那些有趣的 MyClass$1.class 文件,而且当你想要使匿名类成为单例或需要一个句柄时,通常你必须重构,或者某物.我也只是不喜欢你在另一个函数调用过程中以你刚刚编写的东西的形式附加处理程序的方式.只是让我觉得很奇怪.:-)

By anonymous classes I mean those declared inline in a function in that weird Java way. This is often seen in examples of Swing code where you write a button handler inline with the declaration of it. I don't really like anonymous classes because you get those funny MyClass$1.class files and more often than not you'll have to refactor later when it turns out you want to make the anonymous class a singleton or need a handle to it or something. I also just don't like the way you're attaching a handler in the form of something you just made up whilst in the middle of another function call. It just makes me feel weird. :-)

私有类是您在文件底部编写的完全在公共类声明之外的类.我更喜欢私有类,因为它们在源文件和类之间没有很强的 1:1 关系的其他语言中有意义.我没有那种奇怪的感觉,我知道(或者我认为我知道)Java 编译器将如何处理该类的声明.

Private classes are the ones you write at the bottom of your file completely outside the declaration of your public class. I prefer private classes just because they make sense in other languages where there isn't a strong 1:1 relationship between a source file and a class. I don't get that weird feeling and I know (or I think I know) how a Java compiler will handle the declaration of that class.

嵌套类是写在公共类的波浪线中的类.与其他两个相比,我真的不知道嵌套类意味着什么.:-)

Nested classes are ones written inside the squigglies of your public class. I really don't know what a nested class means compared to the other two. :-)

推荐答案

谁能解释一下Java中匿名类、嵌套类和私有类的区别?

  • 匿名类是例如 Runnable 中的

    • Anonymous classes are for instance the Runnable in

      new Thread(new Runnable() {
          public void run() {
              ...
          }
      }.start();
      

    • 嵌套类看起来像

    • Nested classes look like

      class SomeClass {
      
          ...
      
          class NestedClass {
              ....
          }
      }
      

    • 私有类(您将其称为完全在公共类声明之外写在文件底部的类")实际上是包范围的类.您不能在类前面使用 private 修饰符,除非它是嵌套的!

    • Private classes (which you refer to as "the ones you write at the bottom of your file completely outside the declaration of your public class") are actually package scoped classes. You can't have the private modifier in front of a class, unless it is nested!

      我想知道与每个相关的运行时成本以及每个的编译器方法,以便我可以掌握哪个最适合用于例如性能(编译器优化的潜力)、内存使用以及其他 Java 编码人员的普遍接受度.

      我怀疑这些方法之间是否存在性能差异.为什么?因为每种方法最终都会被编译成一个单独的或多或少的常规"类.(或多或少"是因为在某些情况下会生成访问方法,但它没有副作用,并且很可能由 JIT 内联.)

      I doubt that there is any performance difference between these approaches. Why? Because each approach ends up as being compiled into a separate more or less "regular" class. ("More or less" due to the fact that an accesss-method is generated in some cases, but it's side effect free and most likely inlined by the JIT anyway.)

      此代码:

      public class TestOuter {
      
          int fieldOuter;
          public void methodOuter() {
          }
      
          class TestInner {
              int fieldInner;
              public void methodInner() {
              }
          }
      }
      
      
      class TestPackage {
      
          int fieldPackage;
          public void methodPackage() {
          }
      
      }
      

      被编译成:

      $ javap -c TestOuter$TestInner
      Compiled from "TestOuter.java"
      public class TestOuter extends java.lang.Object{
      int fieldOuter;
      
      public TestOuter();
        Code:
         0:   aload_0
         1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
         4:   return
      
      public void methodOuter();
        Code:
         0:   return
      
      }
      

      <小时>

      $ javap -c TestOuter
      Compiled from "TestOuter.java"
      public class TestOuter extends java.lang.Object{
      int fieldOuter;
      
      public TestOuter();
        Code:
         0:   aload_0
         1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
         4:   return
      
      public void methodOuter();
        Code:
         0:   return
      
      }
      

      <小时>

      $ javap -c TestPackage
      Compiled from "TestOuter.java"
      class TestPackage extends java.lang.Object{
      int fieldPackage;
      
      TestPackage();
        Code:
         0:   aload_0
         1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
         4:   return
      
      public void methodPackage();
        Code:
         0:   return
      
      }
      

      这篇关于Java 类:匿名 vs 嵌套 vs 私有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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