⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 right3_2_1.htm

📁 清华大学JAVA教程
💻 HTM
字号:
<html><head><title>JAVA编程语言</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><link rel="stylesheet" href="../../../css/text.css" type="text/css"><script language="JavaScript"><!--function MM_openBrWindow(theURL,winName,features) { //v2.0  window.open(theURL,winName,features);}//--></script></head><body bgcolor="#FFFFFF" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" ><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">  <tr>     <td valign="top">       <table width="90%" border="0" cellspacing="0" cellpadding="0" align="center" bgcolor="#FFFFFF">        <tr>          <td valign="top">             <p> <span class="pt9-black"> </span><span class="zhongdian">1.类声明:<a name="01"></a></span><span class="pt9-black"><br>              <br>                [public][abstract|final] class className [extends superclassName]               [implements interfaceNameList]<br>                {……}<br>              <br>                其中,修饰符public,abstract,final 说明了类的属性,className为类名,superclassName为类的父类的名字,interfaceNameList为类所实现的接口列表。<br>              <br>               </span><span class="zhongdian">2.类体</span><span class="pt9-black"><a name="02"></a><br>              <br>                类体定义如下:<br>                class className<br>                {[public | protected | private ] [static] <br>                [final] [transient] [volatile] type<br>                variableName;                 <font color="#339900">//成员变量</font><br>                [public | protected | private ] [static]<br>                [final | abstract] [native] [synchronized]<br>                returnType methodName([paramList]) [throws exceptionList]<br>                 {statements}                 <font color="339900">//成员方法</font><br>                }<br>              <br>               </span><span class="zhongdian">3.成员变量<a name="03"></a></span><span class="pt9-black"><br>              <br>                成员变量的声明方式如下:<br>                [public | protected | private ] [static] <br>                [final] [transient] [volatile] type<br>                variableName;                 <font color="339900">//成员变量</font><br>                其中,<br>                static: 静态变量(类变量);相对于实例变量<br>                final: 常量<br>                transient: 暂时性变量,用于对象存档<br>                volatile: 贡献变量,用于并发线程的共享<br>              <br>               </span><span class="zhongdian">4.成员方法<a name="04"></a></span><span class="pt9-black"><br>              <br>                方法的实现包括两部分内容:方法声明和方法体。<br>                [public | protected | private ] [static]<br>                [final | abstract] [native] [synchronized]<br>                returnType methodName([paramList])<br>                [throws exceptionList]            <font color="339900">//方法声明</font><br>                 {statements}                <font color="339900">//方法体</font><br>              <br>                方法声明中的限定词的含义:<br>                static: 类方法,可通过类名直接调用<br>                abstract: 抽象方法,没有方法体<br>                final: 方法不能被重写<br>                native: 集成其它语言的代码<br>                synchronized: 控制多个并发线程的访问<br>              <br>                <b>◇ 方法声明</b><br>                方法声明包括方法名、返回类型和外部参数。其中参数的类型可以是简单数据类型,也可以是复合数据类型(又称引用数据类型)。<br>              <br>                对于简单数据类型来说,java实现的是值传递,方法接收参数的值,但不能改变这些参数的值。如果要改变参数的值,则用引用数据类型,因为引用数据类型传递给方法的是数据在内存中的地址,方法中对数据的操作可以改变数据的值。<br>              <br>                例3-1说明了简单数据类型与引用数据的区别。<br>              <br>              <img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题">【例3-1】<a name="07"></a><br>                import java.io.*;<br>                public class PassTest{<br>                float ptValue;<br>                public static void main(String args[]) {<br>                int val;<br>                PassTest pt=new PassTest();<br>                val=11;<br>                System.out.println(&quot;Original Int Value is:&quot;+val); <br>                pt.changeInt(val);                   <font color="339900">//值参数</font><br>                System.out.println(&quot;Int Value after Change is:&quot; +val);               <font color="339900">/*值参数<br>                                  值的修改,没有影响值参数的值*/</font><br>                pt.ptValue=101f;<br>                System.out.println(&quot;Original ptValue is:&quot;+pt.ptValue);<br>                pt.changeObjValue(pt); <font color="339900">//引用类型的参数</font><br>                System.out.println(&quot;ptValue after Change is:&quot;+pt.ptValue);<font color="339900">/*               引用参数值的修改,改变了引用参数的值*/</font> <br>                }<br>                public void changeInt(int value){<br>                value=55;            <font color="339900">//在方法内部对值参数进行了修改</font><br>                }<br>                public void changeObjValue(PassTest ref){<br>                ref.ptValue=99f;        <font color="339900">//在方法内部对引用参数进行了修改</font><br>                  }<br>                }<br>              <br>                 <font color="#0000FF"><font color="#990000"><a href="#07"onClick="MM_openBrWindow('tanchu1.htm','','width=200,height=150')"><font color="#FF0000">查看运行结果</font></a></font></font><br>              </span></p>            <p class="pt9-black">   <b>◇ 方法体</b><br>                方法体是对方法的实现,它包括局部变量的声明以及所有合法的Java指令。方法体中声明的局部变量的作用域在该方法内部。若局部变量与类的成员变量同名,则类的成员变量被隐藏。<br>              <br>                例3-2 说明了局部变量z和类成员变量z的作用域是不同的。<br>              <br>              <span class="pt9-black"><img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"></span>【例3-2】<a name="08"></a><br>                import java.io.*;<br>                class Variable{<br>                int x=0,y=0,z=0;              <font color="339900">//类的成员变量</font><br>                void init(int x,int y) {<br>                this.x=x; this.y=y; <br>                int z=5;                <font color="339900"> //局部变量</font><br>                System.out.println(&quot;** in init**&quot;);<br>                System.out.println(&quot;x=&quot;+x+&quot; y=&quot;+y+&quot; z=&quot;+z);<br>                  } <br>                }<br>                public class VariableTest{<br>                public static void main(String args[]){<br>                Variable v=new Variable();<br>                System.out.println(&quot;**before init**&quot;);<br>                System.out.println(&quot;x=&quot;+v.x+&quot; y=&quot;+ v.y+&quot;               z=&quot;+v.z);<br>                v.init(20,30);<br>                System.out.println(&quot;**after init**&quot;);<br>                System.out.println(&quot;x=&quot;+v.x+ &quot; y=&quot;+ v.y+&quot;               z=&quot;+v.z);<br>                  }<br>                }<br>              <br>                 <a href="#08" onClick="MM_openBrWindow('tanchu2.htm','','width=200,height=150')"><font color="#FF0000">查看运行结果</font></a><br>              <br>                上例中我们用到了this,这是因为init()方法的参数名与类的成员变量x,y的名字相同,而参数名会隐藏成员变量,所以在方法中,为了区别参数和类的成员变量,我们必须使用this。this-----用在一个方法中引用当前对象,它的值是调用该方法的对象。返回值须与返回类型一致,或者完全相同,或是其子类。当返回类型是接口时,返回值必须实现该接口。<br>              <br>               <span class="zhongdian">5.方法重载<a name="05"></a></span><br>               <br>                方法重载是指多个方法享有相同的名字,但是这些方法的参数必须不同,或者是参数的个数不同,或者是参数类型不同。返回类型不能用来区分重载的方法。<br>              <br>                参数类型的区分度一定要足够,例如不能是同一简单类型的参数,如int与long。<br>              <br>              <span class="pt9-black"><img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"></span>【例3-3】<a name="09"></a><br>                import java.io.*;<br>                class MethodOverloading{<br>                void receive(int i) {<br>                System.out.println(&quot;Receive one int data&quot;);<br>                System.out.println(&quot;i=&quot;+i);<br>                }<br>                void receive(int x, int y) {<br>                System.out.println(&quot;Receive two int datas&quot;);<br>                System.out.println(&quot;x=&quot;+x+&quot; y=&quot;+y);<br>                  } <br>                }<br>                public class MethodOverloadingTest{<br>                public static void main(String args[]) {<br>                MethodOverloading mo=new MethodOverloading();<br>                mo.receive(1);<br>                mo.receive(2,3);<br>              <br>                  } <br>                }<br>              <br>                <a href="#09" onClick="MM_openBrWindow('tanchu3.htm','','width=200,height=150')"><font color="#FF0000">查看运行结果</font></a>(编译器会根据参数的个数和类型来决定当前所使用的方法)<br>            </p>            <p class="pt9-black"> <span class="zhongdian">6. 构造方法<a name="06"></a></span><br>              <br>                <b>◇</b> 构造方法是一个特殊的方法。Java 中的每个类都有构造方法,用来初始化该类的一个对象。<br>                <b>◇</b> 构造方法具有和类名相同的名称,而且不返回任何数据类型。<br>                <b>◇</b> 重载经常用于构造方法。<br>                <b>◇</b> 构造方法只能由new运算符调用<br>              <br>              <span class="pt9-black"><img src="../../../images/html/liti.gif" width="38" height="38" align="absbottom" title="例题"></span>【例3-4】<br>                class Point{<br>                int x,y;<br>                Point(){<br>                x=0; y=0;<br>                }<br>                Point(int x, int y){<br>                this.x=x; <br>                this.y=y;<br>                  }<br>                }</p>            </td>        </tr>      </table>    </td>  </tr></table></body></html>

⌨️ 快捷键说明

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