开发者

check reference class type at compile time

开发者 https://www.devze.com 2023-01-08 10:07 出处:网络
If I have some type decla开发者_高级运维rations like: JCheckBoxMenuItem t1; JRadioButtonMenuItem t2;

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消