java - Subclassing a generic type, returning instances of the subclass from a method in another class -


It was a simple, brilliant idea. Use the power of Java 5.0 animated type to encode the description of the data dictionary (attribute name, type, category, units, etc.) and create a type-safe system for setting and reading attribute values ​​(i,. E, in characteristic AAH summarized, ACC is calculated and should accept the values ​​of ACC001, ACC002, ACC003, etc. Only.)

The bottleneck is that different types of different characteristics (floods) Scale, float, text, enumerated), and each type of behavior varies, so I create a base class with a type parameter and some abstract methods:

  public abstract Class GsAttributeValueBase & lt; T Comparable & lt; T & gt; & Gt; {Protected T m_value; ... public GsAttributeValueBase (...) {..} ... public abstract zero setValue (T value) IllegalArgumentException throws; Public T GetValue () {Return m_value; } // etc, etc., etc} I then subclass it for each type (basically, I'm trying to duplicate partial expertise):  
 < Code> Public Class GSATTualValvestor Extended GSATitulateValuebase & gt; Small & gt; {...} Public Sector GsAttributeValueLong GsAttributeValueBase & lt; Long & gt; {...} Public Class GsAttributeValueEncoded GsAttributeValueBase & lt; GsAttributeEncodedValueEnum & gt; {...} ...  

so far is good

  public GsAttributeValueBase & lt; Like to return an example of some subtypes, I basically want to make factory method in the specialty counting type; ? Comparative & lt ;? & Gt; & Gt; CreateInstance () {Switch (m_format) {Case Short: Back to the new GSATIVEViewert (...); Case Long: Back to the new GSATIVE WELL LOL (...); Case encoded: New GSATTUval Value Encoded (...); ...}}  

and call this method:

  GsAttributeValueShort = GsAttributeEnum.AAH.CreateInstance ();  

This is where I hit a brick wall; I found

 : GsAttributeValueBase & lt; Receives an incompatible type error on capture order? Java.lang.Comparable Extended & lt ;? & Gt; & Gt; I have tried almost a dozen settings on the announcement of CreateInstance () so far (this can not be fixed, because it depends on specific information for the frequency of calculation). I'm tear out your hair at this point; I've ruined this rabbit hole for several days, and today or need to work or fully pants fully.  

I really want to do this work; I think it will not be valued only for this project, but other projects are moving forward. But Java does not behave like generic C ++ templates (to some extent, who went home with a vendetta last week), and I know that I am missing something important here, but I do not know what it is is.

Edit

I can not make a way to think about this work in my head and I have burned too much time for it. Thanks for the suggestions, but I'm going to move on and shutting it down. I

Edit 2

Oh I do not stop my own question Oh well

about it:

  public & lt; T comparative & lt; T & gt; ; & Gt; GsAttributeValueBase & lt;? Super T & gt; CreateInstance () {...}  

Comments

Popular posts from this blog

Eclipse CDT variable colors in editor -

AJAX doesn't send POST query -

wpf - Custom Message Box Advice -