Topic: OOP program: PHP code like J2ME code.
PHP code:
<?php
Main::startApp();
/*
*入口类
*/
class Main
{
public static function startApp()
{
$lyj = new WapLyj();
try {
$lyj->test();//调用方法
} catch(MyError $e) {
echo $e->toString();//捕获错误
}
}
public static function pauseApp() { }
public static function distroyApp() { }
}
/*
*类一
*/
class WapLyj
{
var $url = '';
public function test()
{
if ($this->url == '')
throw new MyError('变量$url未初始化值为“www.waplyj.in”');//抛出错误
else
echo '变量$url的值为'.$this->url;
}
}
/*
*类二
*/
class MyError extends Exception
{
var $msg = '';
function __construct($msg)
{
$this->msg = $msg;
}
public function toString()
{
return $this->msg;
}
}
?>
J2ME code:
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.Display;
import javax.microedition.io.ConnectionNotFoundException;
/*
*入口类
*/
public class Main extends MIDlet
{
protected void startApp()
{
WapLyj lyj = new WapLyj();
try {
lyj.test();//调用方法
} catch(MyError e) {
e.toString();//捕获错误
}
}
protected void pauseApp() { }
protected void destroyApp(boolean arg0) { }
}
/*
*类一
*/
class WapLyj
{
String url = "";
public void test() throws MyError
{
if (this.url == "")
throw new MyError("变量url未初始化值为“www.waplyj.in”");//抛出错误
else
{
try {
new Main().platformRequest(this.url);
} catch(ConnectionNotFoundException cnfe) { }
}
}
}
/*
*类二
*/
class MyError extends Exception
{
String msg = "";
public MyError(String msg)
{
this.msg = msg;
}
public String toString()
{
return this.msg;
}
}