隨筆 - 20  文章 - 57  trackbacks - 0
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          51CTO

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜


               構造方法
               構造方法是一種特殊的方法,沒有返回值,函數名必須與類名相同。
               一個類里可以創建多個構造方法,如果不創建,系統會自動創建一個空的沒有參數的構造方法。下面由一個例子來說明構造方法中參數不同時的作用:



          class Employee{
              //設置Employee類的屬性
              private int id;
              private String name;
              private double price;
              private String branch;
              //無參的構造方法
              Employee(){
                  System.out.println("設置無參信息");
              }
              //可不可以把判斷輸入信息的方法寫到構造函數中?
              Employee(int id){
                  if(this.id>0)
                  {
                      this.id = id;
                      System.out.println("設置單參信息    " + this.id);
                  }
                  else{
                      System.out.println("輸入有誤!");
                  }
                 
              }
              Employee(int id, String name){
                  this.id = id;
                  this.name = name;
                  System.out.println("設置雙參信息    " + this.id + "   " + this.name);
              }
             
              Employee(int id, String name, double price, String branch){
                  //在這個構造方法中,JVM會先調用setId方法來給id屬性數值
                  this.setId(id);
                  this.setBranch(name);
                  this.setPrice(price);
                  this.setBranch(branch);
                 
          //        this.id = id;
          //        this.name = name;
          //        this.price = price;
          //        this.branch = branch;
                  System.out.println("設置四參信息    " + this.id + "   " + this.name + "  " + this.price + "  " + this.branch);;
              }
              public int getId() {
                  return id;
              }
              public void setId(int id) {
                  this.id = id;
              }
              public String getName() {
                  return name;
              }
              public void setName(String name) {
                  this.name = name;
              }
              public double getPrice() {
                  return price;
              }
              public void setPrice(double price) {
                  this.price = price;
              }
              public String getBranch() {
                  return branch;
              }
              public void setBranch(String branch) {
                  this.branch = branch;
              }





          public class Workers {
              public static void main(String[] args){
                  //聲明一個對象,并實例化
                  Employee employee1 = null;
                  System.out.println("******************************");
                  employee1 = new Employee();
                  Employee employee2 = new Employee(-2010);
                  Employee employee3 = new Employee(2010, "員工");
                  Employee employee4 = new Employee(2010, "員工2", 45000, "技術部");
              }

          }

          這個程序的輸出結果是:
                             ******************************
                             設置無參信息
                             輸入有誤!
                             設置雙參信息    2010   員工
                             設置四參信息    2010   null  45000.0  技術部


          這個程序能夠說明,構造方法是在一個對象被實例化的時候被調用的。

          在構造方法中,我還有個問題:
                        可不可以把判斷輸入信息是否正確的方法寫到構造函數中?
                        判斷輸入信息是否正確寫在set方法中和寫在構造方法中有什么不同的效果。

                以下是我自己的看法不知道是否正確?
                        我覺得把判斷信息寫在構造方法中和set方法中的作用是一樣的,只不過寫在set方法中必須把構造方法中的賦值語句由this.屬性名=屬性名   換為  this.set屬性名(屬性名)就可以了!


                         但不知道這樣做可不可以,從程序上來說是可以的,但在實際中這樣做是否可行,還請大蝦們指教。

                       
          posted on 2010-10-14 00:52 tovep 閱讀(815) 評論(0)  編輯  收藏

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


          網站導航:
           
          主頁
          主站蜘蛛池模板: 芮城县| 读书| 安宁市| 三江| 贡觉县| 波密县| 茌平县| 瓦房店市| 高尔夫| 磐安县| 津市市| 望城县| 海阳市| 赤壁市| 开江县| 阿城市| 冀州市| 平山县| 萨嘎县| 双牌县| 崇左市| 宁都县| 卢湾区| 徐水县| 阿图什市| 广平县| 剑河县| 安丘市| 兴仁县| 永泰县| 布尔津县| 高邮市| 西林县| 运城市| 诸暨市| 化州市| 江孜县| 定结县| 拉萨市| 高尔夫| 陇南市|