java traits or mixins pattern?

前端 未结 8 2193
小蘑菇
小蘑菇 2020-12-16 14:02

Is there a way to emulate mixins or traits in java? basically, I need a way to do multiple inheritance so I can add common business logic to several classes

8条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-16 14:21

    Java's answer to multiple inheritance is the ability to implement multiple interfaces. Of course, this means you'll get the method declarations, but not the logic.

    You could try emulating mixins by composition: your Java class could define member variables that represent other classes that perform some common business logic.

    In designing Java classes, I have not found the lack of C++ style multiple inheritance to inhibit the design of my architecture. You will find a way to achieve what you want to do.

提交回复
热议问题