Nested/Inner class in external file

后端 未结 6 698
醉话见心
醉话见心 2021-01-07 15:57

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

public class MyClass {
  ...
  public class MyNestedClass {
             


        
相关标签:
6条回答
  • 2021-01-07 16:33

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

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

    0 讨论(0)
  • 2021-01-07 16:39

    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.

    0 讨论(0)
  • 2021-01-07 16:42

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

    0 讨论(0)
  • 2021-01-07 16:43

    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.

    0 讨论(0)
  • 2021-01-07 16:48

    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.

    0 讨论(0)
  • 2021-01-07 16:59

    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

    0 讨论(0)
提交回复
热议问题