黑马程序员技术交流社区

标题: 主函数运行问题 [打印本页]

作者: stormdzh    时间: 2013-9-24 16:12
标题: 主函数运行问题
我的主函数里面使先传入label的,为什么先打印的使Button!

  1. <DIV class=blockcode>
  2. <BLOCKQUOTE>import java.awt.*;

  3. class awtTest
  4. {
  5. public static void main(String[] args)
  6. {
  7. Label label=new Label("LLLL");
  8. myMethod(label);


  9. Button button=new Button("BBBB");
  10. myMethod(button);
  11. }

  12. public static void myMethod(Component c)
  13. {
  14. if(c instanceof Button)
  15. {
  16. Button Buttonc=(Button)c;
  17. Buttonc.setLabel("i'm a label");
  18. System.out.println(Buttonc.getLabel());

  19. }
  20. else if(c instanceof Label)
  21. {
  22. Label Labelc=(Label)c;
  23. Labelc.setText("i'm a button");
  24. System.out.println(Labelc.getText());

  25. }
  26. }
  27. }
复制代码
运行结果:

QQ截图20130924155012.png (2.97 KB, 下载次数: 10)

QQ截图20130924155012.png

作者: 杨晓燕    时间: 2013-9-24 16:18
因为你说   是button就打印出label   ,所以   ...
作者: 梁贺    时间: 2013-9-24 16:32
26.else if(c instanceof Label)

27.{

28.Label Labelc=(Label)c;

29.Labelc.setText("i'm a button");

30.System.out.println(Labelc.getText());

31.

32.}
这里你传入的是Lable,然后再这你设置的是Labelc.setText("i'm a button");所以打印的是button.~
作者: HM代景康    时间: 2013-9-24 18:10
因为你这里输入的是29.Labelc.setText("i'm a button");你先输入lable就会想输出这个,





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2