In Java, can we divide a class into multiple files

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

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

2条回答
  •  野性不改
    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.

提交回复
热议问题