java Java本地类的优势

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/478804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:34:07  来源:igfitidea点击:

Advantage of Local Classes Java

javaoopclasslocal

提问by Paradius

What is the advantage of local classes in Java or in any other language that makes use of this feature?

Java 或任何其他使用此功能的语言中的本地类有什么优势?

采纳答案by geowa4

They allow you to take logic out of the parent class and objectify it. This removes functionality from where it doesn't belong and puts it into its own class. But what if this new object is only needed for a short time, only for the duration of a single block of code? Well, that's where a local class fits in.

它们允许您从父类中取出逻辑并将其对象化。这会从不属于它的地方删除功能并将其放入自己的类中。但是如果这个新对象只在很短的时间内需要,只需要一个代码块的持续时间呢?嗯,这就是当地班级适合的地方。

回答by coobird

Here's an example of how an anonymous inner class, a local inner class, and a regular inner class might be present in a program. The example is looking at a myMethodmethod and a InnerClassclass present in MyClassclass. For the sake of discussion, those classes will all be implementing the Runnableinterface:

下面是一个匿名内部类、本地内部类和常规内部类如何出现在程序中的示例。该示例正在查看类中存在的myMethod方法和类。为了便于讨论,这些类都将实现接口:InnerClassMyClassRunnable

public class MyClass
{
    public void myMethod()
    {
        // Anonymous inner class        
        Runnable r = new Runnable() {
            public void run() {}
        };

        // Local inner class
        class LocalClass implements Runnable
        {
            public void run() {}
        }
    }

    // ... //

    // Inner class
    class InnerClass implements Runnable
    {
        public void run() {}
    }
}

The anonymous inner classcan be used to simply to make an class that implements Runnablewithout actually having to write out the class and naming it, and as krosenvoldmentioned in his post, it is used as a "poor man's closure" in Java.

匿名内部类可以用来简单地做一个实现的类Runnable,而不必实际写出来的类,并将其命名,并作为krosenvold在他的文章中提到,它是作为一个在Java中“穷人的关闭”。

For example, a very very simple way to start a Threadusing an anonymous inner class would be:

例如,Thread使用匿名内部类启动一个非常非常简单的方法是:

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

An local inner classcan be used to make a class that is within the local scope -- it won't be able to be accessed from other methods outside of myMethod.

一个局部内部类可以被用来制造一类是本地范围内-这将无法从以外的其他方法来访问myMethod

If there was another method and we tried to make an instance of the LocalClassthat is located inside the myMethodmethod, we won't be able to do so:

如果有另一种方法并且我们尝试创建LocalClass位于该myMethod方法内部的的实例,我们将无法这样做:

public void anotherMethod()
{
    // LocalClass is out of scope, so it won't be found,
    // therefore can't instantiate.
    new Thread(new LocalClass()).start();
}

An inner classis part of the class that the inner class is located in. So, for example, the inner class InnerClasscan be accessed from other classes by MyClass.InnerClass. Of course, it also means that another method in MyClasscan instantiate an inner class as well.

一个内部类是内部类位于类的一部分。因此,举例来说,内类InnerClass可以从其它类通过访问MyClass.InnerClass。当然,这也意味着另一个方法MyClass也可以实例化一个内部类。

public void anotherMethod()
{
    // InnerClass is part of this MyClass. Perfectly instantiable.
    new Thread(new InnerClass()).start();
}

Another thing about the anonymous inner class and local inner class is that it will be able to access finalvariables which are declared in the myMethod:

关于匿名内部类和本地内部类的另一件事是,它将能够访问final在 中声明的变量myMethod

public void myMethod()
{
    // Variable to access from anonymous and local inner classes.
    final int myNumber = 42;

    // Anonymous inner class        
    Runnable r = new Runnable() {
        public void run()
        {
            System.out.println(myNumber); // Works
        }
    };

    // Local inner class
    class LocalClass implements Runnable
    {
        public void run()
        {
            System.out.println(myNumber); // Works
        }
    }

    // ... //

So, what are the advantages? Using anonymous inner classes and local inner classes instead of having a separate full-blown inner class or class will allow the former to have access to finalvariables in the method in which they are declared, and at the same time, the classes are local to the method itself, so it can't be accessed from outside classes and other methods within the same class.

那么,有哪些优势呢?使用匿名内部类和本地内部类而不是单独的成熟的内部类或类将允许前者访问final声明它们的方法中的变量,同时,这些类是本地的方法本身,因此不能从外部类和同一类中的其他方法访问它。

回答by Tom Hawtin - tackline

There are a number of things that you can do with local classes that you don't get with anonymous inner classes.

您可以使用本地类执行许多匿名内部类无法实现的操作。

  • because the type has a name, you can more usefully add members not in the supertype
  • a given name can make stack traces easier to follow (particularly for lock objects)
  • subtype more than one base type
  • construct in more than one place, and multiple constructors
  • 因为类型有一个名字,你可以更有用地添加不在超类型中的成员
  • 给定的名称可以使堆栈跟踪更容易跟踪(特别是对于锁定对象)
  • 亚型不止一种基型
  • 在一个以上的地方构造,以及多个构造函数

On the other hand they make some hideously verbose syntax even messier.

另一方面,它们使一些极其冗长的语法变得更加混乱。