java - How to access subclass static methods from Class token? -


the concept i'm having trouble how can pass token (such apple.class) method, , access subclass's static methods can retrieve generic information subclass?(in case generic image of apple)

here's java-like psudocode:

abstract class food { }           class apple extends food {   static image getimage(){     return (generic image of apple);   } } class onion extends food {   static image getimage(){     return (generic image of onion);   } }  void arraymanager(class<? extends food> foodtoken){   useimage(foodtoken.getimage()); // <-- can't access static methods class token   storetype(foodtoken); }  //... executing code ... arraymanager(apple.class); arraymanager(onion.class); 

the executing code needs tell arraymanager() kinds of foods wants stored. arraymanager() method displays simple generic image , stores token later reference. can't find way generic image of specific subclass.

if arraymanager() method wanted instantiate passed in token, no problem, just:

foodtoken.newinstance(); 

and call non-static version of getimage().

but seems absurd have instantiate food type object (either before arraymanager() call or afterward) when want static method give me generic image not associated specific apple instance. if store token , instantiate temporarily retrieve image, have instantiate , throw object away every time need static method of subclass. if instantiate , store actual object instead of token, i'm faced storing thousands of identical objects generic information.

i've been struggling versions of issue weeks. far haven't found seems include issue of token having pass method. if methodology wet i'm open other ways of going things. i'm hoping don't have use bulky piece of reflection code. thank reading.

you can reflection: (assuming you're in same package)

method m = foodtoken.getmethod("getimage"); image image = (image) m.invoke(null); 

if you're outside of package, methods may not accessible (as defined in example, @ least), can use reflection fix that, too. use getdeclaredmethod instead of getmethod in case, , call setaccessible(true) make accessible caller.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -