數(shù)據(jù)加載中……

          2008年3月5日

          練習(xí)題,長(zhǎng)方形求周長(zhǎng)和面積

          定義一個(gè)長(zhǎng)方形,求它的周長(zhǎng)和面積。用面向?qū)ο蟮姆椒ā?

          ?

          class 長(zhǎng)方形 {

          ????int 長(zhǎng);int ;

          ????int 周長(zhǎng)()

          ????{

          ????????return 2*(長(zhǎng)+);

          ????}

          ????int 面積()

          ????{

          ????????return 長(zhǎng)*;

          ????}????????

          ????public static void main(String[] args)

          ????{

          ????????長(zhǎng)方形 chang1=new 長(zhǎng)方形();

          ????????長(zhǎng)方形 chang2=new 長(zhǎng)方形();

          ????????chang1.長(zhǎng)=10;

          ????????chang1.=5;

          ????????System.out.println("周長(zhǎng)="+chang1.周長(zhǎng)());

          ????????System.out.println("面積="+chang1.面積());

          ????????chang2.長(zhǎng)=20;

          ????????chang2.=8;

          ????????System.out.println("周長(zhǎng)="+chang2.周長(zhǎng)());

          ????????System.out.println("面積="+chang2.面積());

          ????}

          }

          posted @ 2008-03-20 11:59 rick 閱讀(1620) | 評(píng)論 (0)編輯 收藏
          繼承

          public class Animal

          {

          ????int height;

          ????int weight;

          ????void animal()

          ????{

          ????????System.out.println("Animal constract");

          ????}

          ????void eat()

          ????{

          ????????System.out.println("Animal eat");

          ????}

          ????void sleep()

          ????{

          ????????System.out.println("Animal sleep");

          ????}

          ????void breathe()

          ????{

          ????????System.out.println("Animal breathe");

          ????}

          }

          /*

          * 理解繼承是理解面向?qū)ο蟪绦蛟O(shè)計(jì)的關(guān)鍵

          * 在java中,通過關(guān)鍵字extends繼承一個(gè)已有的類,被繼承的類稱為父類(超類,基類),新的類稱為子類(派生類)。

          * * 在java中,不允許多繼承

          */

          class Fish extends Animal

          {

          ????void fish()

          ????{

          ????????

          ????????System.out.println("fish constract");

          ????}

          ????void breathe()

          ????{

          ????????//super.breathe();

          ????????//super.height=40;

          ????????System.out.println("fish boo");

          ????}

          }

          class Integration

          {

          ????public static void main(String[]args)

          ????{

          ????????//Animal an=new Animal();

          ????????Fish fh=new Fish();

          ????????//an.breathe();

          ????????//fh.height=30;

          ????????fh.breathe();

          ????????

          ????}

          }

          /*

          *在子類當(dāng)中定義一個(gè)與父類同名,返回類型,參數(shù)類型均一致的方法,稱為方法的覆蓋

          *方法的覆蓋發(fā)生在子類和父類之間。

          *調(diào)用父類的方法使用super

          */

          /*特殊變量super,提供了父類的訪問

          * 可以使用super訪問被父類被子類隱藏的變量或覆蓋的方法

          * 每個(gè)子類構(gòu)造方法的第一句,都是隱藏的調(diào)用super(),如果父類沒有這種形式的構(gòu)造函數(shù),那么在編譯器中就會(huì)報(bào)錯(cuò)。

          *

          *

          *

          */

          posted @ 2008-03-20 11:58 rick 閱讀(193) | 評(píng)論 (0)編輯 收藏
          關(guān)鍵字static和final

          靜態(tài)方法和靜態(tài)變量是屬于某一個(gè)類,而不屬于類的對(duì)象。

          靜態(tài)方法和靜態(tài)變量的引用直接通過類名引用。

          在靜態(tài)方法中不能調(diào)用非靜態(tài)的方法和引用非靜態(tài)的成員變量。反之,則可以。

          可以用類的對(duì)象obj去調(diào)用靜態(tài)的方法method(),如:obj.method()。

          ?

          Final在聲明時(shí)需要進(jìn)行初始化。

          使用關(guān)鍵字final定義常量,例如:final double PI=3.1415926

          作為一種約定,在定義常量時(shí),通常采用大寫的形式。

          Final常量可以在聲明的同時(shí)賦初值,也可以在構(gòu)造函數(shù)中賦初值。

          為了節(jié)省內(nèi)存,我們通常將常量聲明為靜態(tài)的(static)

          ?

          在聲明為static時(shí),就要在聲明final常量時(shí)進(jìn)行初始化。

          static final double //PI=3.1415926;

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????PI=3.1415926;

          ????????x=a;

          ????????y=b;

          ????}

          這種方式是錯(cuò)誤的。

          正確的方法如下:

          static final double PI=3.1415926;

          ?

          posted @ 2008-03-05 17:13 rick 閱讀(270) | 評(píng)論 (0)編輯 收藏
          特殊變量this

          This變量表示成員對(duì)象本身。

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????point()

          ????{????????

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????void output(int x,int y)

          ????{

          ????????this.x=x;

          ????????this.y=y;

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????/*pt=new point();

          ????????{

          ????????????

          ????????????pt.output();????????????

          ????????}*/

          ????????pt=new point(3,3);

          ????????{

          ????????????pt.output(5,5);

          ????????????pt.output();

          ????????}

          ????}

          }

          當(dāng)類中有2個(gè)同名變量,一個(gè)屬于類(類的成員變量),而另一個(gè)屬于某個(gè)特定的方法(方法中的局部變量),使用this區(qū)分成員變量和局部變量。

          使用this簡(jiǎn)化構(gòu)造函數(shù)的調(diào)用。

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????point()

          ????{????

          ????????this(1,1);

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????void output(int x,int y)

          ????{

          ????????this.x=x;

          ????????this.y=y;

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????pt.output();

          ????}

          }

          我們使用一個(gè)不帶參數(shù)的構(gòu)造方法來調(diào)用帶參數(shù)的構(gòu)造方法,在不帶參數(shù)的構(gòu)造方法中使用this(1,1);this本身表示pt對(duì)象,他調(diào)用帶參數(shù)的成員方法,來給x和y賦值。大大簡(jiǎn)化了調(diào)用方法。

          在一個(gè)類中所有的實(shí)例(對(duì)象)調(diào)用的成員方法在內(nèi)存中只有一份拷貝,盡管在內(nèi)存中可能有多個(gè)對(duì)象,而數(shù)據(jù)成員(實(shí)例變量,成員變量)在類的每個(gè)對(duì)象所在的內(nèi)存中都存在著一份拷貝。This變量允許相同的實(shí)例方法為不同的對(duì)象工作。每當(dāng)調(diào)用一個(gè)實(shí)例方法時(shí),this變量將被設(shè)置成引用該實(shí)例方法的特定的類對(duì)象。方法的代碼接著會(huì)與this所代表的對(duì)象的特定數(shù)據(jù)建立關(guān)聯(lián)。

          posted @ 2008-03-05 13:57 rick 閱讀(178) | 評(píng)論 (0)編輯 收藏
          面向?qū)ο蟮姆椒?/a>

          面向?qū)ο蟮姆椒ㄒ冉ㄒ粋€(gè)類,這個(gè)類相當(dāng)于一個(gè)模板,然后要為這個(gè)類實(shí)例化一個(gè)對(duì)象。然后對(duì)這個(gè)對(duì)象才能進(jìn)行操作。

          類具有狀態(tài)和行為的方式。

          狀態(tài)就像人這個(gè)類的狀態(tài)有身高和體重,行為有吃飯這個(gè)行為。

          下面用一個(gè)點(diǎn)來說明

          public class point

          {

          ????int x,y;

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????{

          ????????????pt.x=10;

          ????????????pt.y=10;

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          構(gòu)造函數(shù),構(gòu)造函數(shù)和類的方法類似。構(gòu)造方法的名字和類名相同,并且沒有返回值,構(gòu)造方法主要為類的對(duì)象定義初始化狀態(tài)。

          我們不能直接調(diào)用構(gòu)造函數(shù),只能通過new關(guān)鍵字來調(diào)用從而創(chuàng)建類的實(shí)例

          Java的類都要求有構(gòu)造方法,如果沒有定義構(gòu)造方法,則java會(huì)默認(rèn)使用一個(gè)缺省的方法,就是不帶參數(shù)的方法。

          public class point

          {

          ????int x,y;

          ????point()

          ????{

          ????????x=5;

          ????????y=10;

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          對(duì)于構(gòu)造方法,還可以使用參數(shù)的方法,在實(shí)例化對(duì)象的時(shí)候,直接傳遞參數(shù)就可以了

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point(3,3);

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          New關(guān)鍵字的作用

          為對(duì)象分配內(nèi)存空間。

          引起對(duì)象構(gòu)造方法的調(diào)用。

          為對(duì)象返回一個(gè)引用。

          ?

          各種數(shù)據(jù)類型的默認(rèn)值是:

          數(shù)值型: 0

          Boolean: false

          Char: "\0"

          對(duì)象: null

          ?

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point(3,3);

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????}

          }

          輸出是0 0

          帶參數(shù)的構(gòu)造方法和不帶參數(shù)的構(gòu)造方法可以同時(shí)使用。只要參數(shù)類型或參數(shù)個(gè)數(shù)不同。在調(diào)用是是通過對(duì)指定參數(shù)類型和參數(shù)個(gè)數(shù)的方法來調(diào)用哪個(gè)構(gòu)造方法。

          ?

          ?

          這就是方法的重載(overload):重載構(gòu)成的條件:方法的名稱相同,但參數(shù)類型或參數(shù)個(gè)數(shù)不同,才能構(gòu)成方法的重載。

          public class point

          {

          ????int x,y;

          ????point(int a,int b)

          ????{

          ????????x=a;

          ????????y=b;

          ????}

          ????point()

          ????{

          ????????

          ????}

          ????

          ????void output()

          ????{

          ????System.out.println(x);

          ????System.out.println(y);

          ????}

          ????public static void main(String[] args)

          ????{

          ????????point pt;

          ????????pt=new point();

          ????????{

          ????????????pt.output();????????????

          ????????}

          ????????/*pt=new point(3,3);

          ????????{

          ????????????pt.output();

          ????????}*/

          ????}

          }

          這2種方法都是可以使用的。

          主站蜘蛛池模板: 澄迈县| 南乐县| 开鲁县| 伊吾县| 颍上县| 资溪县| 义马市| 安丘市| 怀远县| 光山县| 博野县| 晋中市| 大名县| 康马县| 天长市| 景洪市| 茌平县| 泽普县| 岳西县| 百色市| 利津县| 岳池县| 扶余县| 鄄城县| 札达县| 财经| 萝北县| 宁南县| 雷山县| 巴林右旗| 蒙城县| 洛阳市| 绥宁县| 桂阳县| 横峰县| 富阳市| 石楼县| 深州市| 通渭县| 沙洋县| 逊克县|