现在的位置: 首页 > 编程语言 > 正文

怎样创建你自己的JAVA类?怎样使用你自己的JAVA类

2020年05月29日 编程语言 ⁄ 共 1429字 ⁄ 字号 评论关闭

  创建和使用你自己的JAVA类创建你自己的JAVA类非常容易。下面学步园小编来讲解下怎样创建你自己的JAVA类?怎样使用你自己的JAVA类?

  怎样创建你自己的JAVA类

  新建一个phptest.java文件,将它放置在你的java.class.path目录下,文件内容如下:

  publicclassphptest{

  /**

  *AsampleofaclassthatcanworkwithPHP

  *NB:Thewholeclassmustbepublictowork,

  *andofcoursethemethodsyouwishtocall

  *directly.

  *

  *AlsonotethatfromPHPthemainmethod

  *willnotbecalled

  */

  publicStringfoo;

  /**

  *Takesastringandreturnstheresult

  *oramsgsayingyourstringwasempty

  */

  publicStringtest(Stringstr){

  if(str.equals("")){

  str="Yourstringwasempty.";

  }

  returnstr;

  }

  /**

  *whatisfoo()simplyreturnsthevalueofthevariablefoo.

  */

  publicStringwhatisfoo(){

  return"foois"+foo;

  }

  /**

  *Thisiscalledifphptestisrunfromthecommandlinewith

  *somethinglike

  *javaphptest

  *or

  *javaphptesthellothere

  */

  publicstaticvoidmain(Stringargs[]){

  phptestp=newphptest();

  if(args.length==0){

  Stringarg="";

  System.out.println(p.test(arg));

  }else{

  for(inti=0;i".$myj->test("HelloWorld")."";

  $myj->foo="AStringValue";

  echo"Youhavesetfooto".$myj->foo."
n";

  echo"Myjavamethodreports:".$myj->whatisfoo()."
n";

  ?>

  怎样使用你自己的JAVA类

  如果你得到这样的警告信息:java.lang.ClassNotFoundExceptionerror,这就意味着你的phptest.class文件不在你的java.class.path目录下。

  注意的是JAVA是一种强制类型语言,而PHP不是,这样我们在将它们融合时,容易导致错误,于是我们在向JAVA传递变量时,要正确指定好变量的类型。如:$myj->foo=(string)12345678;or$myj->foo="12345678";

  这只是一个很小的例子,你可以创建你自己的JAVA类,并使用PHP很好的调用它!

  以上就是关于“怎样创建你自己的JAVA类?怎样使用你自己的JAVA类”的内容,希望对大家有用。更多资讯请关注学步园。学步园,您学习IT技术的优质平台!

抱歉!评论已关闭.