iT邦幫忙

0

java 手機程式設計繼承問題

  • 分享至 

  • xImage

最近被繼承和介面還有抽象給搞混了>"<
1.請問一下為什麼繼承midlet,一定要有startApp()、pauseApp()、destroyApp()這三個方法?我知道繼承介面和抽象的方式一定要實作方法,但下面這些沒有用到繼承介面或是抽象方法,為什麼一定要有這3個方法呢?
2.http://java.sun.com/j2se/1.3/docs/api/index.html 這是sun看api的地方
我想知道例如java.applet當我要使用它時,要怎看要哪些方法一定要用到,就像上面繼承midlet一樣,一定要用到3個方法不然一定編譯錯誤?
以下是程式碼,請前輩導正我的觀念>"< 謝謝
import javax.microedition.midlet.*;
public class HelloMIDlet extends MIDlet
{
public HelloMIDlet()
{
//建構式
}
public void startApp()
{
}
public void pauseApp()
{
}
public void destroyApp(boolean unconditional)
{
}
}

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

24
bizpro
iT邦大師 1 級 ‧ 2009-06-03 22:04:19
最佳解答

這是MidLet的原始碼, 是一個abstract class
1
2 /*
3 * MicroEmulator
4 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
5 *
6 * This library is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation; either version 2.1 of the License, or (at your
9 * option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 * for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 package javax.microedition.midlet;
22
23 import javax.microedition.lcdui.Display;
24
25 import com.barteo.emulator.MIDletAccess;
26 import com.barteo.emulator.MIDletBridge;
27
28 public abstract class MIDlet
29 {
30 private boolean destroyed;
31
32 class MIDletAccessor extends MIDletAccess
33 {
34 public MIDletAccessor(MIDlet amidlet)
35 {
36 super(amidlet);
37 }
38
39 public void startApp() throws MIDletStateChangeException
40 {
41 MIDletBridge.setCurrentMIDlet(midlet);
42 getDisplayAccess().updateCommands();
43 midlet.startApp();
44 }
45
46 public void pauseApp()
47 {
48 midlet.pauseApp();
49 }
50
51 public void destroyApp(boolean unconditional) throws MIDletStateChangeException
52 {
53 if (!destroyed) {
54 midlet.destroyApp(unconditional);
55 }
56 getDisplayAccess().clean();
57 }
58 }
59
60
61 protected MIDlet()
62 {
63 MIDletBridge.setAccess(this, new MIDletAccessor(this));
64
65 // initialize display
66 Display.getDisplay(this);
67 destroyed = false;
68 }
69
70
71 protected abstract void startApp() throws MIDletStateChangeException;
72
73 protected abstract void pauseApp();
74
75 protected abstract void destroyApp(boolean unconditional) throws MIDletStateChangeException;
76
77
78 public final String getAppProperty(String key)
79 {
80 return MIDletBridge.getAppProperty(key);
81 }
82
83
84 public final void notifyDestroyed()
85 {
86 destroyed = true;
87 MIDletBridge.notifyDestroyed();
88 }
89
90
91 public final void notifyPaused()
92 {
93 }
94
95 }

您可以看到行71, 73, 和75是三個abstract的method(s). 這三個method(s)是管理這個MIDlet的生命週期(life cycle). 這也是abstract的重點. 而會用abstract主要是JME是在極有限的資源中運算時的保護機制, 要求程式運作在JME的生命周期的管制中.

看更多先前的回應...收起先前的回應...
honjam iT邦新手 5 級 ‧ 2009-06-03 22:08:54 檢舉

原來藏在這裡,那請教一下,在api的文件裡怎看到有沒有abstract或是 介面呢?

bizpro iT邦大師 1 級 ‧ 2009-06-03 22:26:39 檢舉

這是javax.microedition.midlet.MIDLet的文件:
http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/midlet/MIDlet.html
很清楚的也可以看到
public abstract class MIDlet
extends Object

honjam iT邦新手 5 級 ‧ 2009-06-03 22:36:40 檢舉

我知道了,不管是哪個東西只要是在文件的Method Summary裡看到有abstract、interface這些都是要實作的方法囉

bizpro iT邦大師 1 級 ‧ 2009-06-03 23:28:55 檢舉

其實您只要用IDE, 如eclipse, netbeans, IntelliJ IDEA, 等等, 這些都會告訴您要怎麼做的.

我要發表回答

立即登入回答