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

查看:31
本文介绍了在 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生成一个class文件
  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. It's 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 );
       }
  }); 

如果您需要创建一堆这些并使用它们做一些事情,您可以将其更改为

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