數(shù)據(jù)加載中……
          面向?qū)ο蟮姆椒?/a>

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

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

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

          下面用一個點來說明

          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)造方法主要為類的對象定義初始化狀態(tài)。

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

          Java的類都要求有構(gòu)造方法,如果沒有定義構(gòu)造方法,則java會默認(rèn)使用一個缺省的方法,就是不帶參數(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();????????????

          ????????}

          ????}

          }

          對于構(gòu)造方法,還可以使用參數(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)鍵字的作用

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

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

          為對象返回一個引用。

          ?

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

          數(shù)值型: 0

          Boolean: false

          Char: "\0"

          對象: 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ù)不同。在調(diào)用是是通過對指定參數(shù)類型和參數(shù)個數(shù)的方法來調(diào)用哪個構(gòu)造方法。

          ?

          ?

          這就是方法的重載(overload):重載構(gòu)成的條件:方法的名稱相同,但參數(shù)類型或參數(shù)個數(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種方法都是可以使用的。

          posted on 2008-03-05 11:58 rick 閱讀(232) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 盱眙县| 武定县| 柏乡县| 望谟县| 邢台市| 平定县| 西林县| 乐安县| 大连市| 宜宾市| 泽州县| 桂平市| 铜川市| 大化| 大余县| 南康市| 休宁县| 桂平市| 灵宝市| 蒙阴县| 常熟市| 深水埗区| 东阿县| 郧西县| 金阳县| 定远县| 固镇县| 达拉特旗| 堆龙德庆县| 四川省| 吉林市| 蓝田县| 馆陶县| 玛多县| 奉新县| 洪雅县| 玛沁县| 中宁县| 通辽市| 湖口县| 喜德县|