Nested/Inner class in external file

2019-04-17 18:52发布

I have a class MyClass and an inner class MyNestedClass like this:

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.

Is there a way to achieve that?

6条回答
在下西门庆
2楼-- · 2019-04-17 19:21

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.

查看更多
我想做一个坏孩纸
3楼-- · 2019-04-17 19:21

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

查看更多
虎瘦雄心在
4楼-- · 2019-04-17 19:28

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.

查看更多
小情绪 Triste *
5楼-- · 2019-04-17 19:30

Objects of inner classes keep implicit references to the objects of the paren class. If the nested class 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 not declare it public (out of that package can not be accessed).

查看更多
仙女界的扛把子
6楼-- · 2019-04-17 19:31

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.lang or java.util.

in pkg/MyClass.java

public class MyClass {
  ...
}

in 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.

查看更多
淡お忘
7楼-- · 2019-04-17 19:35

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

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

查看更多
登录 后发表回答