數據加載中……
          面向對象的方法

          面向對象的方法要先建一個類,這個類相當于一個模板,然后要為這個類實例化一個對象。然后對這個對象才能進行操作。

          類具有狀態和行為的方式。

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

          下面用一個點來說明

          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();????????????

          ????????}

          ????}

          }

          構造函數,構造函數和類的方法類似。構造方法的名字和類名相同,并且沒有返回值,構造方法主要為類的對象定義初始化狀態。

          我們不能直接調用構造函數,只能通過new關鍵字來調用從而創建類的實例

          Java的類都要求有構造方法,如果沒有定義構造方法,則java會默認使用一個缺省的方法,就是不帶參數的方法。

          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();????????????

          ????????}

          ????}

          }

          對于構造方法,還可以使用參數的方法,在實例化對象的時候,直接傳遞參數就可以了

          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關鍵字的作用

          為對象分配內存空間。

          引起對象構造方法的調用。

          為對象返回一個引用。

          ?

          各種數據類型的默認值是:

          數值型: 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

          帶參數的構造方法和不帶參數的構造方法可以同時使用。只要參數類型或參數個數不同。在調用是是通過對指定參數類型和參數個數的方法來調用哪個構造方法。

          ?

          ?

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

          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 閱讀(230) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 彭阳县| 二手房| 商城县| 莱州市| 涡阳县| 府谷县| 突泉县| 兰溪市| 洪洞县| 敦化市| 张家港市| 德化县| 泽州县| 福海县| 云浮市| 英山县| 前郭尔| 陇川县| 永春县| 碌曲县| 崇明县| 和平县| 张家川| 萝北县| 大新县| 手机| 五常市| 泰宁县| 丹江口市| 噶尔县| 安多县| 雷波县| 水富县| 黄平县| 宜兰市| 曲靖市| 滨州市| 额尔古纳市| 渑池县| 甘南县| 台东市|