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

查看:150
本文介绍了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 in

    • 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 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天全站免登陆