Java内部类扩展外部类 [英] Java Inner Class extends Outer Class

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

问题描述

在Java中,有些情况下内部类扩展了外部类.

There are some cases in Java where an inner class extends an outer class.

例如,java.awt.geom.Arc2D.Float是java.awt.geom.Arc2D的内部类, 并且扩展了Arc2D. (请参见 http://download.oracle .com/javase/6/docs/api/java/awt/geom/Arc2D.Float.html )

For example, java.awt.geom.Arc2D.Float is an inner class of java.awt.geom.Arc2D, and also extends Arc2D. (c.f. http://download.oracle.com/javase/6/docs/api/java/awt/geom/Arc2D.Float.html)

此外,sun.org.mozilla.javascript.internal.FunctionNode.Jump扩展了sun.org.mozilla.javascript.internal.Node,后者是FunctionNode的超类. (很抱歉,无法找到指向Javadoc的链接)

Also, sun.org.mozilla.javascript.internal.FunctionNode.Jump extends sun.org.mozilla.javascript.internal.Node, which is a superclass of FunctionNode. (sorry... cannot find a link to the javadoc)

对我来说,这似乎很奇怪.然后可以创建这些吗?

To me, this seems odd. Could you then create these?

new Arc2D.Float.Float() //n.b. I couldn't get this to compile in Intellij IDEA;

new FunctionNode.Jump.Jump.Jump(1); // I could get this to compile

将子类嵌套为超类的内部类有什么用?

What purpose does it serve to have a subclass nested as an inner class of the superclass?

我想知道是否要访问超类中的某些内容,但是如果要访问父类中的任何变量/方法,可以使用

I wondered whether it was to access something in the superclass, but if you wanted to access any variables/methods in the parent, you could use

super.variable;

super.method();

jjnguy建议将逻辑放在同一位置.在这种情况下,为什么不编写com.mypackage.AbstractTest文件:

Edit 1: jjnguy has suggested it's to keep the logic in the same place. In which case, why wouldn't you write a file com.mypackage.AbstractTest:

abstract class AbstractTest {
  abstract String getString();
}

class ExtensionTest extends AbstractTest {
  @Override
  String getString() {
    return "hello world";
  }
}

...而不是:

abstract class AbstractTest {
  abstract String getString();

  class ExtensionTest extends AbstractTest {
    @Override
    String getString() {
      return "hello world";
    }
  }
}

正确地指出,我以前的编辑中的建议存在缺陷,因为无法在包外部构造ExtensionTest.但是,在周末我对此进行了进一步的考虑,那么以下内容呢?

Edit 2: It has rightly been pointed out that the suggestion in my previous edit was flawed, as couldn't construct ExtensionTest outside of the package. However, I've had a further think about this over the weekend, so what about the following:

abstract class Test {
  public class ExtensionTest extends AbstractTest {
    @Override
    String getString() {
      return "hello world";
    }
  }

  private abstract class AbstractTest {
    abstract String getString();
  }
} 

从本质上讲,到目前为止,我所看到的最好的答案是让内部类扩展其外部类可以将逻辑分组在一起.但是,我认为无需扩展即可实现.

In essence, the best answer I've seen so far is that having an inner class extend its outer class allows the logic to be grouped together. However, I think that this can be done without the extension.

在我看来,设计一个类,其中可以嵌套无数个相同的子类,这似乎是不好的设计. (上下文:这是在尝试为代码完成实用程序生成字典时出现的,并抛出了StackOverflowException.我找到了一种解决方法,但是我只是不明白为什么要这样设计.)

In my mind, it seems like bad design to have a class that can have an infinite number of the same subclasses nested within it. (Context: this came up whilst trying to produce a dictionary for a code completion utility, and threw a StackOverflowException. I found a workaround, but I just cannot understand why it had been designed that way.)

推荐答案

看看Java的

Have a look at Java's Point2D. It has two inner classes that are sub-classes of it.

要注意的重要一点是它们是static内部类.这与常规内部类完全不同.就像静态方法一样,静态类是在类级别而不是对象级别定义的.

The important thing to note is that they are static inner classes. This has an entirely diffenent meaning that a regular inner class. Just like a static method, a static class is defined at the class-level instead of the object level.

Point2D情况下,已完成逻辑上耦合类及其逻辑的操作.它可以帮助abstract类型Point2D的用户找到他们可以使用的实现.

In the Point2D case, it is done to logically couple the classes and their logic. It helps a user of the abstract type Point2D find an implementation that they can use.

为回应您的编辑,我想指出1个重要的事实.一个Java文件只能包含一个公共类,对于公共内部类,除外.尽管您的两个示例都可以编译,但它们不允许公众访问这些类.如果要在单个文件中向某人展示多个公共类,则必须使用公共静态内部类.

In response to your edit I'd like to point out 1 important fact. A single Java file may only contain one public class, except for public inner classes. While both of your examples may compile, they do not allow access to those classes to the public. If you want to present multiple public classes to someone in a single file, you must use public static inner classes.

这篇关于Java内部类扩展外部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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