可以被重載,不可以被重寫。糾正下,不是重置,是重寫。
銅山網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。成都創(chuàng)新互聯(lián)公司成立于2013年到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
解釋:
一:static方法是不能被重寫的,重寫通常針對的是接口方法,而接口方法中只是進(jìn)行的接口定義,而沒有方法的實(shí)現(xiàn),而static方法肯定是要求有方法體的,所以有沖突。
二:static方法是可以重載的,因?yàn)橹剌d只是定義了方法名相同,其余的一切參數(shù)類型、個(gè)數(shù)、返回值發(fā)生變化都是被允許的,所以是可以重載方法的(實(shí)際上就相當(dāng)于重新創(chuàng)建了一個(gè)靜態(tài)方法)。
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("定點(diǎn)");
private final Button btnSplitReset = new Button("定點(diǎn)重置");
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("繼續(xù)"))) {
// Start the clock!
swThread.go();
btnGo.setLabel("停止");
btnGo.setBackground(Color.red);
} else if (btnGo.getLabel().equals("停止")) {
// Stop the clock!
swThread.noGo();
btnGo.setLabel("繼續(xù)");
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)+ " -- " +
"用時(shí): " + msToString(elapsed) + " -- " +
"沖線時(shí)間: " + 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("時(shí)間:"));
pnlTop.add(lblTime);
pnlTop.add(new Label("計(jì)時(shí):"));
//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("定點(diǎn):"));
pnlTop.add(lblSplit);
pnlTop.add(btnSplit);
pnlTop.add(btnSplitReset);
pnlTop.add(new Label("沖線時(shí)間:"));
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("計(jì)時(shí)器");
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沒有初始化,導(dǎo)致出現(xiàn)了空指針異常,
解決辦法
bt1?=?new?Button("重置");
修改成為
bt2?=?new?Button("重置");
修改后的效果圖
當(dāng)然這段代碼還有一些可以完善的地方 ,修改后如下
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("密 碼");//?密碼中間補(bǔ)充1個(gè)全角的空格?方便和上面的?用戶名三個(gè)字對齊
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);
//點(diǎn)擊窗口右上角的關(guān)閉按鈕時(shí)候?,?結(jié)束程序
addWindowListener(new?WindowAdapter()?{
@Override
public?void?windowClosing(WindowEvent?e)?{
System.exit(0);//結(jié)束程序
}
});
}
public?static?void?main(String[]?args)?{
LoginFrame?lf?=?new?LoginFrame();
}
}
由于JQuery中,提交表單是像下面這樣的:Java代碼 $('#myform').submit() $('#myform').submit() 所以,想當(dāng)然的認(rèn)為,重置表單,當(dāng)然就是像下面這樣子嘍:Java代碼 $('#myform').reset() $('#myform').reset()但是,不幸的是,這樣寫的話,會(huì)有一個(gè)讓你很郁悶的結(jié)果,那就是,表單無法重置!后來,上網(wǎng)查了一下,說是,JQuery中沒有reset方法,經(jīng)核對,果然是沒有。那有么沒有辦法通過JQuery來重置表單呢,答案是有的,不過是一種間接的方法,如下:Java代碼 $('#myform')[0].reset()也就是通過調(diào)用 DOM 中的reset方法來重置表單。
temp=1 是內(nèi)循環(huán)每次重置起始值為1.
流程實(shí)現(xiàn):比如你要算 1 - 5 的階乘之和,你就輸入了5,num就被設(shè)成了5
外循環(huán)從1 循環(huán)到5,
內(nèi)循環(huán) 通過temp 這個(gè)臨時(shí)值分別算 1 到 5 的階乘,
即,第一次從1 乘到 1, 得到 1,然后記在sum里面, sum 變成 1
第二次從1 乘到2,得到2 ,繼續(xù)加在sum里面, sum 變成 3
第三次從1 乘到3,得到6 ,繼續(xù)加在sum里面, sum 變成 9
以此類推
如果temp不重置的話,最后會(huì)算出 1 + 1*1*2 + 2*1*2*3 + 12*1*2*3*4 ......
第二次從1 乘到2,得到2 ,繼續(xù)加在sum里面
函數(shù)重載:
是一個(gè)類中聲明了多個(gè)同名的方法,但有不同的參數(shù)個(gè)數(shù)和參數(shù)類型。
函數(shù)重置:
是指在子類中聲明與父類同名的方法,從而覆蓋了父類的方法。重置解決了子類與父類的差異問題。
例子如下:
//重載
class A {
public d (){};
public d (q){};
public d (q,w){};
}
//重置
class B extends A{
public d (q,f){};
}