If I have some type decla开发者_高级运维rations like:
JCheckBoxMenuItem t1;
JRadioButtonMenuItem t2;
and then a method like
addItem(JMenuItem i)
{
}
can I know if i
is a JCheckBoxMenuItem
or a JRadioButtonMenuItem
type without having
an instance of them when I call it with addItem(t1)
or addItem(t2)
???
If you need different behaviour based on the type of the object, don't use the same method. Make different methods, accepting different argument types.
Well, you can always call t1.getClass()
, whhich will give you the effective class of the input object.
精彩评论