国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

php&java(二)

瀏覽:2日期:2024-02-27 09:10:32
例子1:創(chuàng)建和使用你自己的JAVA類 創(chuàng)建你自己的JAVA類非常容易。新建一個(gè)phptest.java文件,將它放置在你的java.class.path目錄下,文件內(nèi)容如下: 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)建這個(gè)文件后,我們要編譯好這個(gè)文件,在DOS命令行使用javac phptest.java這個(gè)命令。 為了使用PHP測試這個(gè)JAVA類,我們創(chuàng)建一個(gè)phptest.php文件,內(nèi)容如下: <?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是一種強(qiáng)制類型語言,而PHP不是,這樣我們在將它們?nèi)诤蠒r(shí),容易導(dǎo)致錯(cuò)誤,于是我們在向JAVA傳遞變量時(shí),要正確指定好變量的類型。如:$myj->foo = (string) 12345678; or $myj->foo = "12345678"這只是一個(gè)很小的例子,你可以創(chuàng)建你自己的JAVA類,并使用PHP很好的調(diào)用它!
標(biāo)簽: Java
主站蜘蛛池模板: 思南县| 墨江| 澎湖县| 新源县| 龙胜| 济宁市| 文山县| 陆良县| 樟树市| 宜兴市| 含山县| 星座| 肇庆市| 克山县| 京山县| 资阳市| 新田县| 渭南市| 郁南县| 侯马市| 北辰区| 鲁甸县| 淳安县| 象州县| 连云港市| 吉林省| 乌什县| 马山县| 临湘市| 长海县| 仁布县| 镇远县| 偏关县| 宜都市| 类乌齐县| 博客| 武城县| 沙洋县| 仁化县| 昌邑市| 辽宁省|