java.examples.html

来自「php的帮助文档,涉及到PHP的案例和基本语法,以及实际应用内容」· HTML 代码 · 共 115 行

HTML
115
字号
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title>Examples</title>  <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="java.servlet.html">Java Servlet SAPI</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.java.html">Java Functions</a></div> <div class="up"><a href="book.java.html">Java</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Examples</h1> <div id="java.examples-basic" class="section">  <p class="para">   <div class="example">    <p><b>Example #1 Java Example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;get&nbsp;instance&nbsp;of&nbsp;Java&nbsp;class&nbsp;java.lang.System&nbsp;in&nbsp;PHP<br /></span><span style="color: #0000BB">$system&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.lang.System'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;demonstrate&nbsp;property&nbsp;access<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'Java&nbsp;version='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.version'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">'Java&nbsp;vendor='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.vendor'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #DD0000">'OS='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.name'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&nbsp;'&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.version'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&nbsp;on&nbsp;'&nbsp;</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$system</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getProperty</span><span style="color: #007700">(</span><span style="color: #DD0000">'os.arch'</span><span style="color: #007700">)&nbsp;.&nbsp;</span><span style="color: #DD0000">'&nbsp;&lt;br&nbsp;/&gt;'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;java.util.Date&nbsp;example<br /></span><span style="color: #0000BB">$formatter&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.text.SimpleDateFormat'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"EEEE,&nbsp;MMMM&nbsp;dd,&nbsp;yyyy&nbsp;'at'&nbsp;h:mm:ss&nbsp;a&nbsp;zzzz"</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">$formatter</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">format</span><span style="color: #007700">(new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.util.Date'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   <div class="example">    <p><b>Example #2 AWT Example</b></p>    <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;This&nbsp;example&nbsp;is&nbsp;only&nbsp;intended&nbsp;to&nbsp;be&nbsp;run&nbsp;using&nbsp;the&nbsp;CLI.<br /><br /></span><span style="color: #0000BB">$frame&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.awt.Frame'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'PHP'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$button&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.awt.Button'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'Hello&nbsp;Java&nbsp;World!'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'North'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$button</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">validate</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">pack</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">visible&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">True</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$thread&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">Java</span><span style="color: #007700">(</span><span style="color: #DD0000">'java.lang.Thread'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$thread</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sleep</span><span style="color: #007700">(</span><span style="color: #0000BB">10000</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$frame</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">dispose</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>    </div>   </div>   Notes:   <ul class="itemizedlist">    <li class="listitem">     <span class="simpara">      <i>new Java()</i> will create an instance of a class if       a suitable constructor is available. If no parameters are passed and       the default constructor is useful as it provides access to classes       like <i>java.lang.System</i> which expose most of their       functionallity through static methods.     </span>    </li>    <li class="listitem">     <span class="simpara">      Accessing a member of an instance will first look for bean properties      then public fields. In other words, <i>print $date.time</i>      will first attempt to be resolved as <i>$date.getTime()</i>,      then as <i>$date.time</i>.     </span>    </li>    <li class="listitem">     <span class="simpara">      Both static and instance members can be accessed on an object with      the same syntax. Furthermore, if the java object is of type      <i>java.lang.Class</i>, then static members of the class       (fields and methods) can be accessed.     </span>    </li>    <li class="listitem">     <p class="para">      Exceptions raised result in PHP warnings, and <b><tt>NULL</tt></b> results. The      warnings may be eliminated by prefixing the method call with an      &quot;@&quot; sign. The following APIs may be used to retrieve and reset      the last error:      <ul class="itemizedlist">       <li class="listitem"><span class="simpara"><a href="function.java-last-exception-get.html" class="function">java_last_exception_get()</a></span></li>       <li class="listitem"><span class="simpara"><a href="function.java-last-exception-clear.html" class="function">java_last_exception_clear()</a></span></li>      </ul>     </p>    </li>    <li class="listitem">     <span class="simpara">      Overload resolution is in general a hard problem given the      differences in types between the two languages. The PHP Java      extension employs a simple, but fairly effective, metric for      determining which overload is the best match.     </span>     <span class="simpara">      Additionally, method names in PHP are not case sensitive, potentially      increasing the number of overloads to select from.     </span>     <span class="simpara">      Once a method is selected, the parameters are coerced if necessary,       possibly with a loss of data (example: double precision floating point      numbers will be converted to boolean).           </span>    </li>    <li class="listitem">     <span class="simpara">      In the tradition of PHP, arrays and hashtables may pretty much      be used interchangably. Note that hashtables in PHP may only be      indexed by integers or strings; and that arrays of primitive types      in Java can not be sparse. Also note that these constructs are      passed by value, so may be expensive in terms of memory and time.     </span>    </li>   </ul>  </p> </div></div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="java.servlet.html">Java Servlet SAPI</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.java.html">Java Functions</a></div> <div class="up"><a href="book.java.html">Java</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?