I don't know how to express this best using classes and TS.
- Parent does not have property x.
- child 1 has private property x, and a method using it.
- child 2 has private property x, and the SAME method using it.
How do I define the method in the parent to decrease code repetition, while keeping the property x, specific to the children?
Welcome to a problem that OOP introduces - constantly coming up with new classes with arbitrary names in order to find a common parent for inheritance. What you probably need is composition instead of inheritance. youtube.com/watch?v=wfMtDGfHWpA In classes, re-using methods is not possible (unless you inherit them). The only abstraction layer you have are objects and classes. you either HAVE to redeclare them - duplicating yourself - or use inheritance.
Обсуждают сегодня