c# - Inheritance vs varying instances? -
if modelling various brands of cars use inheritance hierarchy, or varying constructor parameters?
what general rule whether relate objects using inheritance, or re-using same class?
for cars new car("porsche","991","3.8") or have overall abstract car superclass, abstract subclass manufacturers "porsche" , possibly class each model of porsche?
if have few properties shared cars (or methods act on object), , unique properties (or methods) each make/model, you'd want use inheritance. otherwise, varying instances fine.
let's want these properties cars:
- make
- model
- year
of doors
in case, wouldn't want create class hierarchy, because doesn't buy anything.
instead, if had 2 "types" of cars: regular , race-car, , race-car enable nitrous oxide (presumably method this), you'd want car class, regularcar , racecar inheriting it.
if you're afraid of having pass same parameters constructor time, can create static methods call constructor on behalf. known factory method pattern.
ps: example came off top of head. hope idea of i'm trying :)
Comments
Post a Comment