Composition instead of inheritance c# | Sololearn: Learn to code for FREE!

0

Composition instead of inheritance c#

I have a class Monster { move() attack() } we can create: melee monster : monster overr attack() ranged monster : monster ovverr attack() flying monster : monster overr move() swimming monster : monster overr move() how to create FlyingRangeMonster and MeleeSwimmingMonster without duplicating code? I suppose we should abandon inheritance and use composition. How to realízate it in this case ? Thank you!

3/12/2021 6:49:01 AM

Tatarochka_

2 Answers

New Answer

+3

How about an interface monster with methods move and attack

0

but if we create an interface we will duplicate code (e.g. melee monster and swimmingmeleemonster will have same realization of attack method)