java - How to compare two classes -
i try create factory, here's code:
public class myfactory { public static <t> mybase create(class<t> _c) { if (_c.getclass().equals(derived1.class)) // (1) return createnewderived1(); return null; } } // caller myfactory.create(derived1.class);
it compiles warning @ line (1)
:
error
'equals()' between objects of inconvertible types 'class<derived1>' , 'class<capture of ? extends class>' <br>reports calls .equals() target , argument of incompatible types. while such call might theoretically useful, represents bug.
and in runtime if
statement fails reason. how can intended behaviour?
public class myfactory { public static <t> mybase create(class<t> _c) { return ( _c == derived1.class ) ? createnewderived1() : null; } } // caller myfactory.create(derived1.class);
see compare class objects further info.
fiy, "real" class object references have class<myclass>
type signatures (before erasure), while class
returned .class
class is, afair, rawtype class
on environments , class<capture of ?>
-ish on others.
Comments
Post a Comment