java: inheritance

2019-04-13 00:20发布

What are some alternatives to inheritance?

7条回答
Root(大扎)
2楼-- · 2019-04-13 00:47

Try Delegation which is also know as Composition

Also found the new alternative as mixin

The mixins are kind of composable abstract classes. They are used in a multi-inheritance context to add services to a class. The multi-inheritance is used to compose your class with as many mixins as you want. For example, if you have a class to represent houses, you can create your house from this class and extend it by inheriting from classes like Garage and Garden. Here is this example written in Scala:

val myHouse = new House with Garage with Garden

You will get more info about mixin https://kerflyn.wordpress.com/2012/07/09/java-8-now-you-have-mixins/

查看更多
登录 后发表回答