In Java, can we divide a class into multiple files

后端 未结 2 867
予麋鹿
予麋鹿 2021-02-11 12:36

Any possibility to divide a class into multiple physical files using Java?

相关标签:
2条回答
  • 2021-02-11 13:20

    No, the whole of a class has to be in a single file in Java.

    If you're thinking of C#'s "partial types" feature, there's no equivalent in Java. (If you weren't thinking of C#, ignore this :)

    0 讨论(0)
  • 2021-02-11 13:22

    This might be a good idea if the class is really so large such that the implemented concepts are not easy to grasp. I see two different ways to do this:

    1. Use inheritance: Move general concepts of the class to a base class and derive a specialized class from it.

    2. Use aggregation: Move parts of your class to a separate class and establish a relationship to the second class using a reference.

    As previously mentioned, there is no concept like partial classes in Java, so you really have to use these OOP mechanisms.

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