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

public class phptest{
/**
* A sample of a class that can work wit " /> 久久精品影视,精品国产第一页,天堂在线网

一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

php&java(二)

例子1:創建和使用你自己的Java
創建你自己的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));   
}
}
}
}

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

為了使用php測試這個Java類,我們創建一個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";

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

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

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

主站蜘蛛池模板: 999影院成 人在线影院 | 亚洲婷婷在线 | 亚洲欧美日韩精品香蕉 | 中文字幕黄色 | 最新eeuss影院在线观看 | 久久人人爽人人爽人人片宅男 | 国产成人福利免费视频 | 中国日韩欧美中文日韩欧美色 | 国产免费一区2区3区4区 | 国产精品久久久久无码av | 品色视频| 国产免费福利 | 久久精品中文字幕第一页 | 伊人网站在线 | 久久国产精品99久久久久久老狼 | 国产精品美女网站 | 狠狠干伊人 | 一级爱做片免费观看久久 | 五月婷婷综合激情网 | 亚洲国产精品成人综合色在线婷婷 | 国产系列 视频二区 | 中文字幕一区2区 | 77777网站| 欧洲精品一区二区三区在线观看 | 看全色黄大色大片免费 | 免费观看黄色网 | 青青国产成人久久91网站站 | 青青草国产免费久久久91 | 性欧美video另类3d | 午夜爽爽性刺激一区二区视频 | 国产精品久久久久久久久久一区 | 热久久最新 | 国产精品原创巨作av | 成人黄网大全在线观看 | 欧美综合视频在线观看 | 色影院在线| 国产有码视频 | 久久精品精品 | 久久综合精品国产一区二区三区 | 国产性生大片免费观看性 | 久久性视频 |