android - How to avoid copy-paste when create similar classes in Java -
i have created custom myaspectbutton
keeps aspect:
public class myaspectbutton extends button { private float m_aspect = -1.f; // ... // ... constructors, setters/getters ... // ... @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { if (aspect >= 0.f) { super.onmeasure(widthmeasurespec, heightmeasurespec); final int measuredwidth = getmeasuredwidth(); widthmeasurespec = measurespec.makemeasurespec(measuredwidth, measurespec.exactly); heightmeasurespec = measurespec.makemeasurespec(math.round(measuredwidth * aspect), measurespec.exactly); } super.onmeasure(widthmeasurespec, heightmeasurespec); } }
now want have myaspectlinearlayout
, myaspectrelativelayout
, , on. onmeasure
method same.
how can implement bunch of these classes little copy-paste possible?
i aware of concept of generic classes in java, here have inherit template parameter compiler not let me do:
public class myaspectwidget<t> extends t { // ... }
does not compile.
if i'm understanding correctly, want inherit bunch of different base classes, apply same behaviour of them:
myaspectbutton extends button myaspectlinearlayout extends linearlayout myaspectrelativelayout extends relativelayout
what looking multiple inheritance, java not support. option have single class performs measuring logic, each of child classes' onmeasure
method call into:
public class myaspectbutton extends button { private float m_aspect = -1.f; @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(mymeasuringclass.measurewidth(this, widthmeasurespec), mymeasuringclass.measureheight(this, heightmeasurespec)); } }
Comments
Post a Comment