to simplify actual code let's there 2 classes, 1 subclass of other:
class chair { val canfold = false; // ... } class foldablechair extends chair { val canfold = true; // ... }
and in implementation have potentially hundreds of other subclasses of chair or foldablechair:
class armchair extends ... {} class deckchair extends ... {} //... etc
for each of these subclasses, suppose each 1 has lengthy implementation want able have extend chair , extend foldablechair - without duplicating code. i'd without having subclass extended. possible somehow? need use traits this?
i'd able create particular instances of subclass extend chair , extend foldablechair, choice made when instantiating it. possible too? thanks!
edit: clarify, want this:
class armchair extends chair {} class armchairfoldable extends foldablechair {}
but implementation of armchair , armchairfoldable same. is, i'd not duplicate implementations.
you can use implementation trait; i.e., trait mix in class , provides additional members implementation.
example:
class chair { // can use def rather val it's constant , // , doesn't need occupy field def canfold = false // ... } class foldablechair extends chair { override def canfold = true // ... } trait extensible extends chair { // trait extends chair mean // applicable chair or subclasses of chair def extend = /* ... */ } class foldableextensiblechair extends foldablechair extensible
then can write:
val = new chair // bare-bones chair // decide @ creation time 1 extensible val b = new chair extensible val c = new foldablechair // non extensible // use predefined class mixes in extensible val d = new foldableextensiblechair
Comments
Post a Comment