Jon Swanson wrote:I can create an interface (see below), but most of the code in the custom text fields is identical (everything but the calls to Convert). Is there a way to extend a class when I have a set of overloaded functions like above? Or a better implementation than just an interface, so I don't duplicate so much code?
Yes.
I hate to say, but you were on the right lines at the start with your enums; you just didn't take it a stage further and link it properly with your other good idea: the interface. And now you've started overthinking...
Go back to your
Weight enum (BTW, don't use all-caps for class names). Why not just add a
convertTo() method and a little "knowledge" to it?
Then it might look something like:
and now
Weight.KILOGRAMS.convertTo(Weight.POUNDS, 3.0)
will return
6.6138670242179628
And now you can take it a stage further. Enums can implement interfaces, so what about:
and then, your
Weight definition becomes:
HIH
Winston