Nested/Inner class in external file

2019-04-17 18:48发布

问题:

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?

回答1:

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.



回答2:

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:

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



回答4:

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

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



回答5:

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



回答6:

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.