可以被重载,不可以被重写。纠正下,不是重置,是重写。
铜山网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、响应式网站等网站项目制作,到程序开发,运营维护。成都创新互联公司成立于2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
解释:
一:static方法是不能被重写的,重写通常针对的是接口方法,而接口方法中只是进行的接口定义,而没有方法的实现,而static方法肯定是要求有方法体的,所以有冲突。
二:static方法是可以重载的,因为重载只是定义了方法名相同,其余的一切参数类型、个数、返回值发生变化都是被允许的,所以是可以重载方法的(实际上就相当于重新创建了一个静态方法)。
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Clock extends Applet {
private final Panel pnlTop = new Panel();
private final Panel pnlBot = new Panel();
private final Label lblDate = new Label();
private final Label lblTime = new Label();
private final Label lblWatch = new Label();
private final Button btnGo = new Button("开始");
private final Button btnReset = new Button("重置");
private final Label lblSplit = new Label();
private final Button btnSplit = new Button("定点");
private final Button btnSplitReset = new Button("定点重置");
private final Button btnLapAdd = new Button("冲线");
private final Button btnLapReset = new Button("冲线重置");
private final java.awt.List lstLaps = new java.awt.List();
private final UpdateClockThread ucThread = new UpdateClockThread();
private final StopwatchThread swThread = new StopwatchThread();
private class btnGoListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
if ((btnGo.getLabel().equals("开始")) ||
(btnGo.getLabel().equals("继续"))) {
// Start the clock!
swThread.go();
btnGo.setLabel("停止");
btnGo.setBackground(Color.red);
} else if (btnGo.getLabel().equals("停止")) {
// Stop the clock!
swThread.noGo();
btnGo.setLabel("继续");
btnGo.setBackground(Color.green);
}
}
}
private class btnResetListener implements ActionListener {
/** Actually run when the button gets clicked.
*@param e The event
*/
public void actionPerformed(ActionEvent e) {
swThread.reset();
btnGo.setLabel("开始");
btnGo.setBackground(Color.green);
}
}
/** Listens to the Split button.
* @version CS2136 - Term D'00 - Assignment 5
* @author Peter Cooper Jr.
*/
private class btnSplitListener implements ActionListener {
/** Actually run when the button gets clicked.
*@param e The event
*/
public void actionPerformed(ActionEvent e) {
lblSplit.setText(lblWatch.getText());
}
}
/** Listens to the Split Reset button.
* @version CS2136 - Term D'00 - Assignment 5
* @author Peter Cooper Jr.
*/
private class btnSplitResetListener implements ActionListener {
/** Actually run when the button gets clicked.
*@param e The event
*/
public void actionPerformed(ActionEvent e) {
lblSplit.setText("");
}
}
/** Listens to the Lap Add button.
* @version CS2136 - Term D'00 - Assignment 5
* @author Peter Cooper Jr.
*/
private class btnLapAddListener implements ActionListener {
/** Actually run when the button gets clicked.
*@param e The event
*/
public void actionPerformed(ActionEvent e) {
swThread.addLap();
}
}
/** Listens to the Lap Reset button.
* @version CS2136 - Term D'00 - Assignment 5
* @author Peter Cooper Jr.
*/
private class btnLapResetListener implements ActionListener {
/** Actually run when the button gets clicked.
*@param e The event
*/
public void actionPerformed(ActionEvent e) {
swThread.resetLap();
}
}
/** A thread that updates the current date time.
* @version CS2136 - Term D'00 - Assignment 5
* @author Peter Cooper Jr.
*/
private class UpdateClockThread extends Thread {
/** The actual work of the thread.
*/
public void run() {
while (true) {
Calendar now = Calendar.getInstance();
String month = Integer.toString(now.get(Calendar.MONTH)+1);
String date = Integer.toString(now.get(Calendar.DAY_OF_MONTH));
String year = Integer.toString(now.get(Calendar.YEAR));
String hour = Integer.toString(now.get(Calendar.HOUR));
if (hour.equals("0")) hour = "12";
String minute = Integer.toString(now.get(Calendar.MINUTE));
if (minute.length() == 1) minute = "0" + minute;
String second = Integer.toString(now.get(Calendar.SECOND));
if (second.length() == 1) second = "0" + second;
String ampm = now.get(Calendar.AM_PM) == Calendar.AM
? "AM" : "PM";
lblDate.setText(month + "/" + date + "/" + year);
lblTime.setText(hour + ":" + minute + ":" + second
+ " " + ampm);
try {
sleep(500);
} catch (InterruptedException e) {}
}
}
}
private class StopwatchThread extends Thread {
/** Whether or not stopwatch is running. */
private boolean going = false;
/** Stores elapsed milliseconds of previous runs. */
private long prevElapsed = 0;
/** Stores beginning time of this run. */
private Date startDate = new Date();
/** Current lap number. */
private int lapNum = 0;
/** Elapsed time at end of last lap. */
private long lastLapTime = 0;
/** Returns elapsed time in milliseconds.
*@return The elapsed time
*/
private long elapsedTime() {
return prevElapsed +
(going ? new Date().getTime() - startDate.getTime() : 0);
}
/** Changes the number of elapsed milliseconds into a string.
*@param time Number of elapsed milliseconds
*@return The elapsed time as a string.
*/
private String msToString(long time) {
String ms, sec, min;
if (time % 10 = 5) //round to nearest hundredth
time += 5;
ms = Long.toString(time % 1000);
while (ms.length() 3)
ms = "0" + ms;
ms = ms.substring(0, ms.length() - 1);
time /= 1000;
sec = Long.toString(time % 60);
if (sec.length() == 1) sec = "0" + sec;
time /= 60;
min = Long.toString(time);
return min + ":" + sec + "." + ms;
}
public void go() {
startDate = new Date();
going = true;
}
public void noGo() {
prevElapsed = elapsedTime();
going = false;
}
public void reset() {
going = false;
prevElapsed = 0;
lastLapTime = 0;
}
public void addLap() {
long elapsed = elapsedTime();
lstLaps.add("冲线 " + Integer.toString(++lapNum)+ " -- " +
"用时: " + msToString(elapsed) + " -- " +
"冲线时间: " + msToString(elapsed - lastLapTime));
lastLapTime = elapsed;
}
/** Resets the lap list.
*/
public void resetLap() {
lstLaps.removeAll();
lapNum = 0;
lastLapTime = 0;
}
/** Main code of the thread.
*/
public void run() {
while (true) {
lblWatch.setText(msToString(elapsedTime()));
yield();
}
}
}
public void init() {
setLayout(new GridLayout(2,1));
setBackground(Color.lightGray);
setForeground(Color.black);
pnlTop.setLayout(new GridLayout(4,4));
pnlTop.add(new Label("日期:"));
pnlTop.add(lblDate);
pnlTop.add(new Label("时间:"));
pnlTop.add(lblTime);
pnlTop.add(new Label("计时:"));
//lblWatch.setBackground(Color.black);
lblWatch.setForeground(Color.blue);
pnlTop.add(lblWatch);
pnlTop.add(btnGo);
btnGo.setBackground(Color.green);
pnlTop.add(btnReset);
pnlTop.add(new Label("定点:"));
pnlTop.add(lblSplit);
pnlTop.add(btnSplit);
pnlTop.add(btnSplitReset);
pnlTop.add(new Label("冲线时间:"));
pnlTop.add(new Label());
pnlTop.add(btnLapAdd);
pnlTop.add(btnLapReset);
pnlBot.setLayout(new GridLayout(1,1));
pnlBot.add(lstLaps);
add(pnlTop);
add(pnlBot);
btnGo.addActionListener(new btnGoListener());
btnReset.addActionListener(new btnResetListener());
btnSplit.addActionListener(new btnSplitListener());
btnSplitReset.addActionListener(new btnSplitResetListener());
btnLapAdd.addActionListener(new btnLapAddListener());
btnLapReset.addActionListener(new btnLapResetListener());
swThread.setDaemon(true);
ucThread.setDaemon(true);
swThread.start();
ucThread.start();
}
public static void main(String[] args) {
Clock applet = new Clock();
Frame aFrame = new Frame("计时器");
aFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
aFrame.add(applet, BorderLayout.CENTER);
aFrame.setSize(400, 200);
applet.init();
applet.start();
aFrame.setVisible(true);
}
}
bt2没有初始化,导致出现了空指针异常,
解决办法
bt1 = new Button("重置");
修改成为
bt2 = new Button("重置");
修改后的效果图
当然这段代码还有一些可以完善的地方 ,修改后如下
import java.awt.Button;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.TextField;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Panel;
public class LoginFrame extends Frame {
Label lb1, lb2;
TextField tf1, tf2;
Button bt1, bt2;
public LoginFrame() {
super("用户登录窗口");
setLayout(new GridLayout(3, 1));
Panel p1 = new Panel();
lb1 = new Label("用户名");
tf1 = new TextField(20);
p1.add(lb1);
p1.add(tf1);
Panel p2 = new Panel();
lb2 = new Label("密 码");// 密码中间补充1个全角的空格 方便和上面的 用户名三个字对齐
tf2 = new TextField(20);
p2.add(lb2);
p2.add(tf2);
Panel p3 = new Panel();
bt1 = new Button("提交");
bt2 = new Button("重置"); //bt1修改成bt2
//bt1 = new Button("重置");
p3.add(bt1);
p3.add(bt2);
add(p1);
add(p2);
add(p3);
pack();
setLocationRelativeTo(null);//窗口居于屏幕中央
setVisible(true);
//点击窗口右上角的关闭按钮时候 , 结束程序
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);//结束程序
}
});
}
public static void main(String[] args) {
LoginFrame lf = new LoginFrame();
}
}
由于JQuery中,提交表单是像下面这样的:Java代码 $('#myform').submit() $('#myform').submit() 所以,想当然的认为,重置表单,当然就是像下面这样子喽:Java代码 $('#myform').reset() $('#myform').reset()但是,不幸的是,这样写的话,会有一个让你很郁闷的结果,那就是,表单无法重置!后来,上网查了一下,说是,JQuery中没有reset方法,经核对,果然是没有。那有么没有办法通过JQuery来重置表单呢,答案是有的,不过是一种间接的方法,如下:Java代码 $('#myform')[0].reset()也就是通过调用 DOM 中的reset方法来重置表单。
temp=1 是内循环每次重置起始值为1.
流程实现:比如你要算 1 - 5 的阶乘之和,你就输入了5,num就被设成了5
外循环从1 循环到5,
内循环 通过temp 这个临时值分别算 1 到 5 的阶乘,
即,第一次从1 乘到 1, 得到 1,然后记在sum里面, sum 变成 1
第二次从1 乘到2,得到2 ,继续加在sum里面, sum 变成 3
第三次从1 乘到3,得到6 ,继续加在sum里面, sum 变成 9
以此类推
如果temp不重置的话,最后会算出 1 + 1*1*2 + 2*1*2*3 + 12*1*2*3*4 ......
第二次从1 乘到2,得到2 ,继续加在sum里面
函数重载:
是一个类中声明了多个同名的方法,但有不同的参数个数和参数类型。
函数重置:
是指在子类中声明与父类同名的方法,从而覆盖了父类的方法。重置解决了子类与父类的差异问题。
例子如下:
//重载
class A {
public d (){};
public d (q){};
public d (q,w){};
}
//重置
class B extends A{
public d (q,f){};
}