Topic: main()方法还捆饶着我 请大家留言指导我下.先谢过.

  Print this page

1.main()方法还捆饶着我 请大家留言指导我下.先谢过. Copy to clipboard
Posted by: iceriver2000
Posted on: 2006-11-08 10:59

问题1>:在一个**.java的文件中定义了一个主类其中包含了3个方法:mathod_01():mathod_02():和main()方法,在运行**.java文件时却只能执行main()方法,要把其他两个方法也执行的话 要怎么处理呢?如果还有副类中的方法又要怎么执行呢?

问题2>: main()是文件执行的入口,如果不包含main()的话 会抛出java.lang.NoSuchMethodError: main的异常 也有人说不包含main()方法能直接运行,但如果能运行的话 那结果在哪里显示出来呢?
问题3>:我运行的**.java文件是却只能显示main()方法中的内容'想要 看到同类中其他方法的内容要怎么处理呢??
恳请大家的指教...一个java初学者.

2.Re:main()方法还捆饶着我 请大家留言指导我下.先谢过. [Re: iceriver2000] Copy to clipboard
Posted by: why
Posted on: 2006-11-08 19:17

问题1>
call (执行) method_01() and method_02() in main()
a. directly if they are public static
b. create an instance of the class and call these 副类中的方法

问题2> 也有人说不包含main()方法能直接运行...
it could be an applet
it could be a static block (but why don't we use main()?
and there could be other tricky ways to do so, but only of academic interest, I believe
otherwsie ask this 有人

问题3> sorry but I don't understand what you meant : 想要看到同类中其他方法的内容
maybe you could provide some code to illustrate the idea

3.Re:main()方法还捆饶着我 请大家留言指导我下.先谢过. [Re: iceriver2000] Copy to clipboard
Posted by: dejinzhao
Posted on: 2006-11-08 23:19

//下面程序无main方法、执行时会抛出异常、但能正常运行
//原理:该程序在“类”装载过程中直接运行、不用再调用main方法进行启动
//如果在该程序中加入main方法如:
/**public static void main (String args[]) {
new TestMain();
}
*/
//将会启动两个程序

public class TestMain implements java.awt.event.ActionListener{
private TestMain() {
javax.swing.JFrame frame=new javax.swing.JFrame();
javax.swing.JButton button=new javax.swing.JButton("click to run");
frame.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
button.addActionListener(this);
label=new javax.swing.JLabel();
label.setPreferredSize(new java.awt.Dimension(500,30));
frame.getContentPane().add(label,java.awt.BorderLayout.SOUTH);
frame.getContentPane().add(button,java.awt.BorderLayout.NORTH);
button.setPreferredSize(new java.awt.Dimension(500,30));
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
frame.setLocation(300,300);
}
private String runMethodOne(){
return "methodOne is executed!";
}
private String runMethodTwo(){
return "methodTwo is executed!";
}
public void actionPerformed(java.awt.event.ActionEvent e){
count++;
System.out.println(runMethodOne()+count+" times");
System.out.println(runMethodTwo()+count+" times");
javax.swing.JButton button=(javax.swing.JButton)e.getSource();
label.setText(runMethodOne()+count+" times , "+runMethodTwo()+count+" times");
}
private javax.swing.JLabel label;
int count;
static{
new TestMain();
}
}

4.Re:main()方法还捆饶着我 请大家留言指导我下.先谢过. [Re: dejinzhao] Copy to clipboard
Posted by: zhangwensheng
Posted on: 2006-11-09 09:38

依据我的理解:
main()的作用是告诉程序 'Please execute from here'
在一个java工程中必须至少有一个main()
而在这样的一个工程中对于一个单独的 class 来说不必要有main()
在上述程序中只有一个class所以main()是必须的否则将抛出异常
这里使用了static关键字 static 是先于main()的,在产生实例之前执行
没有main()仍能继续运行的原因是:static已经完成,当点击按钮时程序去执行相应的模块,不会再理会main()

5.Re:main()方法还捆饶着我 请大家留言指导我下.先谢过. [Re: iceriver2000] Copy to clipboard
Posted by: iceriver2000
Posted on: 2006-11-09 10:23

我大体上有了理性认识了 谢谢大家的热心帮助.


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923