I has a Args
class,this class extend HashMap
and has some methods:
public class Args extends HashMap{
public myMethod_1(){
//...
}
public myMethod_2(){
//...
}
public myMethod_3(){
//...
}
}
now,I want to get a Linked Args
instance,but exnted LinkedHashMap
,
I can't change Args exnteds to LinkedHashMap
,also can't change Args
to a interfaces
can I get a linked Args
instance ?
if not,how the best way to get a LinkedArgs
? (not repeat myMethod_1-3
)
-
2Why can't you change Args to extend LinkedHashMap? Do you realize that LinkedHashMap extends HashMap? So by changing Args to extends LinkedHashMap, you're not giving up any functionality provided by HashMap.Asaph– Asaph03/30/2011 02:33:02Commented Mar 30, 2011 at 2:33
-
2Can you clarify what you're going for? I don't understand why you cannot extend LinkedHashMap instead of just HashMap.Suroot– Suroot03/30/2011 02:36:10Commented Mar 30, 2011 at 2:36
1 Answer 1
The simple solution is change Args
to extend LinkedHashMap
. (I know you said you can't ...)
If that is not an option, then the next best solution is to change Args
to implement Map
and delegate all of the methods in the Map
API (and others as required) to a private LinkedHashMap
instance.
If that is not an option, you could do the same with extends HashMap
. Basically, you override all of the methods in the parent HashMap
, and delegate the actions to a private LinkedHashMap
instance. (It is UGLY but it works ... modulo that each Args
object has a bunch of attributes that are initialized and never used.)