在Java中的方法中使用类定义 [英] Use of class definitions inside a method in Java

查看:246
本文介绍了在Java中的方法中使用类定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

public class TestClass
{
    public static void main(String[] args) 
    {
        TestClass t = new TestClass();
    }

    private static void testMethod(){
        abstract class TestMethod{
            int a;
            int b;
            int c;

            abstract void implementMe();
        }

        class DummyClass extends TestMethod{
            void implementMe(){

            }
        }

        DummyClass dummy = new DummyClass();
    }
}

我发现上面的代码是完美的在Java中合法。我有以下问题。

I found out that the above piece of code is perfectly legal in Java. I have the following questions.


  1. 在方法中使用类定义是什么?

  2. 会为DummyClass生成类文件

  3. 我很难以对象定向的方式想象这个概念。在行为中有一个类定义。

  4. 一个方法中的抽象类对我来说听起来有点疯狂。但不允许接口。这背后有什么原因吗?

  1. What is the use of ever having a class definition inside a method?
  2. Will a class file be generated for DummyClass
  3. Its hard for me to imagine this concept in an Object Oriented manner. Having a class definition inside a behavior. Probably can someone tell me with equivalent real world examples.
  4. Abstract classes inside a method sounds a bit crazy to me. But no interfaces allowed. Is there any reason behind this?


推荐答案

这称为本地类。

2是简单的一个:是,将生成一个类文件。

2 is the easy one: yes, a class file will be generated.

1和3是一样的题。你将使用一个本地类,你永远不需要实例化一个或知道实现细节在任何地方,但在一个方法。

1 and 3 are kind of the same question. You would use a local class where you never need to instantiate one or know about implementation details anywhere but in one method.

典型的用法是创建一个抛弃实现一些接口。例如,你会经常看到这样的:

A typical use would be to create a throw-away implementation of some interface. For example you'll often see something like this:

  //within some method
  taskExecutor.execute( new Runnable() {
       public void run() {
            classWithMethodToFire.doSomething( parameter );
       }
  }); 

如果你需要创建一堆这些并对它们做一些事情, / p>

If you needed to create a bunch of these and do something with them, you might change this to

  //within some method
  class myFirstRunnableClass implements Runnable {
       public void run() {
            classWithMethodToFire.doSomething( parameter );
       }
  }
  class mySecondRunnableClass implements Runnable {
       public void run() {
            classWithMethodToFire.doSomethingElse( parameter );
       }
  }
  taskExecutor.execute(new myFirstRunnableClass());
  taskExecutor.execute(new mySecondRunnableClass());

关于接口:我不知道是否有一个技术问题,使本地定义的接口对于编译器,但即使没有,他们也不会添加任何值。如果在方法外部使用实现本地接口的本地类,则接口将是无意义的。如果一个本地类只在方法内使用,接口和类将在该方法中实现,因此接口定义将是多余的。

Regarding interfaces: I'm not sure if there's a technical issue that makes locally-defined interfaces a problem for the compiler, but even if there isn't, they wouldn't add any value. If a local class that implements a local interface were used outside the method, the interface would be meaningless. And if a local class was only going to be used inside the method, both the interface and the class would be implemented within that method, so the interface definition would be redundant.

这篇关于在Java中的方法中使用类定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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