創(chuàng)建你自己的JAVA類非常容易。新建一個phptest.java文件,將它放置在你的java.class.path目錄下,文件內容如下:

public class phptest{
/**
* A sample of a class that can work wit " /> 国产精品你懂的在线,99视频日韩,国产啊啊啊视频在线观看

色尼玛亚洲综合影院,亚洲3atv精品一区二区三区,麻豆freexxxx性91精品,欧美在线91

php&java(二)

例子1:創(chuàng)建和使用你自己的Java
創(chuàng)建你自己的Java類非常容易。新建一個phptest.Java文件,將它放置在你的Java.class.path目錄下,文件內容如下:

public class phptest{
/**
* A sample of a class that can work with php
* NB: The whole class must be public to work,  
* and of course the methods you wish to call
* directly.
*
* Also note that from php the main method
* will not be called   
*/

public String foo;

/**
* Takes a string and returns the result
* or a msg saying your string was empty
*/
public String test(String str) {    
if(str.equals("")) {
      str = "Your string was empty. ";    
}    
return str;   
}

/**
* whatisfoo() simply returns the value of the variable foo.
*/   
public String whatisfoo() {    
return "foo is " + foo;   
}


/**
* This is called if phptest is run from the command line with
* something like
*  Java phptest
* or
*  Java phptest hello there
*/
public static void main(String args[]) {
phptest p = new phptest();

if(args.length == 0) {
String arg = "";
System.out.println(p.test(arg));
}else{
for (int i=0; i < args.length; i++) {
String arg = args[i];
System.out.println(p.test(arg));   
}
}
}
}

創(chuàng)建這個文件后,我們要編譯好這個文件,在DOS命令行使用Javac phptest.Java這個命令。

為了使用php測試這個Java類,我們創(chuàng)建一個phptest.php文件,內容如下:

<?php

$myj = new Java("phptest");
echo "Test Results are <b>" . $myj->test("Hello World") . "</b>";

$myj->foo = "A String Value";
echo "You have set foo to <b>"   . $myj->foo . "</b><br>n";
echo "My Java method reports: <b>" . $myj->whatisfoo() . "</b><br>n";

?>

如果你得到這樣的警告信息:Java.lang.ClassNotFoundException error ,這就意味著你的phptest.class文件不在你的Java.class.path目錄下。
注意的是Java是一種強制類型語言,而php不是,這樣我們在將它們融合時,容易導致錯誤,于是我們在向Java傳遞變量時,要正確指定好變量的類型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678";

這只是一個很小的例子,你可以創(chuàng)建你自己的Java類,并使用php很好的調用它!

php技術php&amp;amp;java(二),轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 常熟市| 安顺市| 邯郸市| 且末县| 池州市| 讷河市| 梓潼县| 夏河县| 衡山县| 泗阳县| 灵宝市| 扶沟县| 尉犁县| 长葛市| 涟源市| 斗六市| 项城市| 田林县| 长武县| 荣成市| 达拉特旗| 方正县| 凌海市| 乃东县| 海兴县| 万盛区| 建瓯市| 天门市| 聂拉木县| 米脂县| 扎鲁特旗| 滦南县| 高雄市| 松原市| 合水县| 临沂市| 红河县| 丹寨县| 福泉市| 虎林市| 株洲县|