java 外部文件中的嵌套/内部类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6218722/
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-30 14:54:44  来源:igfitidea点击:

Nested/Inner class in external file

javafileexternalnested-class

提问by Alp

I have a class MyClassand an inner class MyNestedClasslike this:

我有一个类MyClass和一个MyNestedClass像这样的内部类:

public class MyClass {
  ...
  public class MyNestedClass {
    ...
  }
}

Both classes are very long. Because of that i'd like to seperate them in two different files, without breaking the hierarchy. This is because the nested class shouldn't be visible to the programmer who uses MyClass.

两节课都很长。因此,我想将它们分成两个不同的文件,而不破坏层次结构。这是因为使用 MyClass 的程序员不应该看到嵌套类。

Is there a way to achieve that?

有没有办法实现这一目标?

采纳答案by x4u

You can make the inner class package private which means that it will only be accessible from other classes in exactly the same package. This is also done quite frequently for hidden classes inside the standard JDK packages like java.langor java.util.

您可以将内部类包设为私有,这意味着只能从完全相同的包中的其他类访问它。这对于标准 JDK 包中的隐藏类(如java.lang或)也经常执行java.util

in pkg/MyClass.java

在 pkg/MyClass.java 中

public class MyClass {
  ...
}

in pkg/MyHiddenClass.java

在 pkg/MyHiddenClass.java 中

class MyHiddenClass {

  final MyClass outer;

  MyHiddenClass( MyClass outer )
  {
      this.outer = outer;
  }
  ...
}

Now when you want to access methods or variables of the outer class you need to prefix them with outer.but you get essentially the same functionality as before when the reference to the outer instance was synthetically created by the compiler.

现在,当您想要访问外部类的方法或变量时,您需要为它们添加前缀,outer.但是当对外部实例的引用由编译器综合创建时,您将获得与以前基本相同的功能。

回答by retrodrone

No. Java source codes can not be split across multiple files. You'd need a construct similar to a partial class as in C#, which Java does not have.

不可以。Java 源代码不能跨多个文件拆分。您需要一个类似于 C# 中的部分类的构造,而 Java 没有。

回答by Op De Cirkel

Objects of innerclasses keep implicit references to the objects of the paren class. If the nestedclass is not static(it is inner) you can not. But if the nested class does not need access to the parent's class instances directly and does not need access to the private fields, than should be ok to refactor, move the inner class out and do notdeclare it public (out of that package can not be accessed).

对象内部类保持到了括号类的对象含蓄地提及。如果嵌套类不是static(它是内部的),则不能。但是如果嵌套类不需要直接访问父类的实例并且不需要访问私有字段,那么重构应该没问题,将内部类移出并且不要将其声明为公共(从那个包中不能被访问)。

回答by osdamv

i think you have a god object or something like that, think in refactor your code

我认为您有一个上帝对象或类似的东西,请考虑重构您的代码

http://en.wikipedia.org/wiki/God_object

http://en.wikipedia.org/wiki/God_object

回答by Grim

Yes, using cascade-jumper. A cascade-jumper is a abstract and non-static inner class that is defined anywhere else.

是的,使用级联跳线。级联跳线是一个抽象的非静态内部类,它在其他任何地方定义。

MyClass.java

我的类

public class MyClass {
  public abstract class CascadeJumper {}
}

MyNestedClass

我的嵌套类

private class MyNestedClass extends MyClass.CascadeJumper {
  MyNestedClass(MyClass dollarOne) {
    dollarOne.super(); // yes, is possible!
  }
}

Regards

问候

回答by rsp

You could create abstract base classes that you extend in both.

您可以创建在两者中扩展的抽象基类。

However, if those classes are large, you might be better off re-evaluating your class design and see if you can refactor into classes which represent one concern each.

然而,如果这些类很大,你最好重新评估你的类设计,看看你是否可以重构为代表一个关注点的类。