编程论坛
400万+工程师在用
华为|鸿蒙开发者日
直播报名
400万+工程师在用
华为|鸿蒙开发者日
直播报名

电子发烧友网工程师

17年用户 16436经验值
擅长:可编程逻辑 MEMS/传感技术 测量仪表 模拟技术 控制/MCU RF/无线
私信 关注

一个简单的Java程序实例

2008-12-6 00:20

<p><span style="FONT-FAMILY: 黑体">一个简单的</span><span lang="EN-US" style="FONT-FAMILY: 'times New="New"  Roman'">Java</span><span style="FONT-FAMILY: 黑体">程序实例</span><br/></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="FONT-FAMILY: 宋体">在系统地学习</span><span lang="EN-US" style="FONT-FAMILY: 宋体">Java</span><span style="FONT-FAMILY: 宋体">语言之前</span><span style="FONT-FAMILY: 宋体">,先回顾</span><span style="FONT-FAMILY: 宋体">一下第2章介绍的简单的</span><span lang="EN-US" style="FONT-FAMILY: 宋体">Java</span><span style="FONT-FAMILY: 宋体">程序,它只实现了main</span><span lang="EN-US" style="FONT-FAMILY: 宋体">()</span><span style="FONT-FAMILY: 宋体">方法。本章以介绍类main</span><span lang="EN-US" style="FONT-FAMILY: 宋体">()</span><span style="FONT-FAMILY: 宋体">方法为主线,阐述Java语言的基本语法,同时介绍用Java编写常用的基本算法。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt; TEXT-ALIGN: left" align="left"><span style="FONT-FAMILY: 宋体">例<b><span lang="EN-US">3-1</span></b><span lang="EN-US"> </span>实现在屏幕上显示“<span lang="EN-US">Hello World</span>”的功能。存放的文件名为<span lang="EN-US">HelloWorld.java</span>。</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 41.95pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">package cha3;</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 41.95pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">class HelloWorld&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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="FONT-FAMILY: 宋体">类首部</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 41.95pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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="FONT-FAMILY: 宋体">类体开始</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 50.95pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">public static void main(String[] args)&nbsp;&nbsp; &nbsp; //</span><span style="FONT-FAMILY: 宋体">类的主方法首部定义</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 55.45pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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="FONT-FAMILY: 宋体">方法体开始</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 59.95pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">//</span><span style="FONT-FAMILY: 宋体">在屏幕上显示″</span><span lang="EN-US">Hello World! !</span><span style="FONT-FAMILY: 宋体">″</span><span lang="EN-US">&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //</span><span style="FONT-FAMILY: 宋体">注释</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 64.45pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">System.out.println(" Hello World! "); //</span><span style="FONT-FAMILY: 宋体">由系统提供的方法</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 59.95pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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="FONT-FAMILY: 宋体">方法体结束</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 41.95pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">}&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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="FONT-FAMILY: 宋体">类体结束</span></p>
<p class="MsoNormalIndent" style="TEXT-JUSTIFY: inter-ideograph; TEXT-INDENT: 55.9pt; LINE-HEIGHT: 15.6pt; TEXT-ALIGN: justify"><span lang="EN-US">&nbsp;</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span lang="EN-US">Java</span><span style="FONT-FAMILY: 宋体">程序由类构成,像例</span><span lang="EN-US">3-1</span><span style="FONT-FAMILY: 宋体">这样简单的类是由类首部、类体两大部分组成的。</span></p>
<h3><b><span style="FONT-FAMILY: 宋体">类首部</span></b></h3>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">类首部</span><span lang="EN-US">class HelloWorld</span><span style="FONT-FAMILY: 宋体">定义类的名字</span><span lang="EN-US">HelloWorld</span><span style="FONT-FAMILY: 宋体">。保留字</span><span lang="EN-US">class</span><span style="FONT-FAMILY: 宋体">和类名之间应至少留有一个空格,类名第一个字符一般大写。<br/></span></p>
<p>&nbsp;</p>
<p><b><span style="FONT-FAMILY: 宋体">类体</span></b> </p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">类体位于类名后面左大括号“{”和结束类体的右大括号“}”之间。类体描述类对象的特征和行为,用Java语言的术语,类体定义类的成员变量(对象、特征和方法(对象行为))。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">例<span lang="EN-US">3-1</span>没有</span><span style="FONT-FAMILY: 宋体">类的属性,它包含主方法</span><span lang="EN-US" style="FONT-FAMILY: 宋体">main()</span><span style="FONT-FAMILY: 宋体">。如果一个类是独立可以运行的程序,必须有</span><span style="FONT-FAMILY: 宋体">主方法</span><span lang="EN-US" style="FONT-FAMILY: 宋体">main()</span><span style="FONT-FAMILY: 宋体">,包括<span lang="EN-US">main()</span>方法的类叫主类。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span lang="EN-US" style="FONT-FAMILY: 宋体">main()</span><span style="FONT-FAMILY: 宋体">方法和其他方法一样,一般分为五个部分</span><span style="FONT-FAMILY: 宋体">。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(<span lang="EN-US">1</span>)</span><span style="FONT-FAMILY: 宋体">方法修饰符。</span><span lang="EN-US" style="FONT-FAMILY: 宋体">main()</span><span style="FONT-FAMILY: 宋体">必须有</span><span lang="EN-US" style="FONT-FAMILY: 宋体">public</span><span style="FONT-FAMILY: 宋体">、</span><span lang="EN-US" style="FONT-FAMILY: 宋体">static </span><span style="FONT-FAMILY: 宋体">修饰符</span><span style="FONT-FAMILY: 宋体">。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(<span lang="EN-US">2</span>)</span><span style="FONT-FAMILY: 宋体">方法返回值。</span><span lang="EN-US" style="FONT-FAMILY: 宋体">main()</span><span style="FONT-FAMILY: 宋体">方法返回</span><span lang="EN-US" style="FONT-FAMILY: 宋体">void</span><span style="FONT-FAMILY: 宋体">,它表示<span lang="EN-US">main()</span></span><span style="FONT-FAMILY: 宋体">方法运行后不返回任何类型的结果。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(<span lang="EN-US">3</span>)</span><span style="FONT-FAMILY: 宋体">方法名。主方法名必须是main</span><span style="FONT-FAMILY: 宋体">;</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(<span lang="EN-US">4</span>)</span><span style="FONT-FAMILY: 宋体">方法形式参数列表。main</span><span lang="EN-US" style="FONT-FAMILY: 宋体">()</span><span style="FONT-FAMILY: 宋体">的</span><span style="FONT-FAMILY: 宋体">形式参数必须是类型为String的数组,形式参数名可以按 Java标识符规则定义。</span><span lang="EN-US" style="FONT-FAMILY: 宋体"> </span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(<span lang="EN-US">5</span>)</span><span style="FONT-FAMILY: 宋体">方法体</span><span style="FONT-FAMILY: 宋体">。描述方法的功能。</span></p>
<p>
<script language="JavaScript">
  <!--
   menuwrite();
   window.onscroll=respos;
   respos();
   -->
  </script>
</p>

更多回帖

打开APP