android - Java - Typecast a object to a String Class Named Object -


this question has answer here:

i have object of class. want typecast class. so, -

heavyvehicle hv = new heavyvehicle(); truck tr = (truck) hv; 

if typecast hv object truck classes object.

but if name of class stored in string this-

string tocaststringname = "truck"; 

is there way typecast hv object class named tocaststringname?

i'm not sure use case can use:

class.forname(classname).cast(someobject) 

to dynamically cast object knowing class name. in case, code:

heavyvehicle hv = new heavyvehicle(); string tocaststringname = "truck"; class.forname(tocaststringname).cast(hv); 

would return object of type truck.

there's long discussion uses , tradeoffs of either approach in post: java class.cast() vs. cast operator


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -