日期:2014-05-20  浏览次数:20752 次

请教:关于J2me中类的正确调用问题
各位高手,对于J2me手机编程,我是个初学者,以前是用VB编程的。我想从一个类调用另一个类中的方法,编写了如下程序,不知错在哪里,请高手指正。因为我是菜鸟,请务必给出正确代码,先谢谢了。

第一个类如下:
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*; 

public class HelloWorld extends MIDlet implements CommandListener { 
private Display display; 
public static final Command exitCommand = new Command( "退出 ",Command.EXIT,1); 

public HelloWorld(){} 

public void startApp(){ 
if(display == null){ 
display = Display.getDisplay(this); 

TextBox t = new TextBox( "数据核算 ", "2007年财务数据", 100, 0); 
String s = t.getString();
  int c1=2;
  int c2=53;
  int c3=0;
  sz sz=new sz();
  int c4=sz.jf(c1,c2,c3);  
  s = s + "\n" + "合计:" + c4; 
t.setString(s);  
t.addCommand(exitCommand); 
t.setCommandListener(this); 
display.setCurrent(t); 


public void pauseApp(){} 
public void destroyApp(boolean unconditional){} 
public void commandAction(Command cmd,Displayable displayable){ 
if(cmd == exitCommand){ 
destroyApp(false); 
notifyDestroyed(); 


}


另一个类代码如下:
import javax.microedition.midlet.*; 
import javax.microedition.lcdui.*;

public class sz extends MIDlet {
public void startApp(){

public int jf(int a,int b,int c) {
int aa=a;
int bb=b;
int cc=aa*bb;
c=cc;
return c;
}

public void pauseApp(){} 
public void destroyApp(boolean unconditional){}
}



------解决方案--------------------
MIDlet类不能显式实例化,必须由MIDlet管理器来实例化并启动

比如你的类sz,其实和MIDlet没有任何关系的,不需要继承MIDlet

Java code

public class sz{
    public int jf(int a, int b, int c) {
        int aa = a;
        int bb = b;
        int cc = aa * bb;
        c = cc;
        return c;
    }
}