2017-02-20 23 views
0

我正在使用Java作爲初學者,我正在學習一個教程(http://www.javatpoint.com/object-and-class-in-java),其中指出可以通過三種不同的方式將對象初始化爲java。在Java中通過三種不同方式初始化對象有什麼用途?

  1. 通過引用變量
  2. 通過方法
  3. 通過構造

我的問題是,什麼是使用三種不同的方法對單個任務的意義。

實施例: 1)對象和類示例:初始化通過參考

class Student{ 
    int id; 
    String name; 
    } 
    class TestStudent2{ 
    public static void main(String args[]){ 
     Student s1=new Student(); 
     s1.id=101; 
     s1.name="Sonoo"; 
     System.out.println(s1.id+" "+s1.name);//printing members with a white space 
    } 
    } 

2)對象和類示例:初始化通過方法

class Student{ 
    int rollno; 
    String name; 
    void insertRecord(int r, String n){ 
     rollno=r; 
     name=n; 
    } 
    void displayInformation(){System.out.println(rollno+" "+name);} 
    } 
    class TestStudent4{ 
    public static void main(String args[]){ 
     Student s1=new Student(); 
     Student s2=new Student(); 
     s1.insertRecord(111,"Karan"); 
     s2.insertRecord(222,"Aryan"); 
     s1.displayInformation(); 
     s2.displayInformation(); 
    } 
    } 

3)對象和類示例:初始化通過構造函數

class Employee{ 
     int id; 
     String name; 
     float salary; 
     void insert(int i, String n, float s) { 
      id=i; 
      name=n; 
      salary=s; 
     } 
     void display(){System.out.println(id+" "+name+" "+salary);} 
    } 
    public class TestEmployee { 
    public static void main(String[] args) { 
     Employee e1=new Employee(); 
     Employee e2=new Employee(); 
     Employee e3=new Employee(); 
     e1.insert(101,"ajeet",45000); 
     e2.insert(102,"irfan",25000); 
     e3.insert(103,"nakul",55000); 
     e1.display(); 
     e2.display(); 
     e3.display(); 
    } 
    } 
+1

嗯,是什麼?.....你有代碼示例爲1,2,3來顯示你的意思,因爲我不完全是你在說什麼 – Antoniossss

+0

你能解釋你到底需要什麼嗎? – Akshay

+0

問題更新 – jax

回答

0

對於單個任務使用三種不同方法有什麼意義。

這與軟件開發中的任何任務具有相同的意義:通常有多種編寫代碼的方式可能會導致相同的最終狀態,因此我們考慮哪一種最適合。可以說(恕我直言),這是發展的真正技能:不能簡單地使電腦一件特別的事情,但編寫代碼來做這件事情,儘可能簡單/乾淨/可理解/可擴展。

在這個特定情況下,我會強烈建議通過構造函數初始化字段。有時候,對象的字段是可變的,並且在對象創建後被初始化是有意義的(如情況1和2)。但是在大多數情況下 - 特別是在這裏帶有定義對象本身的「id」字段 - 如果對象是用這些字段構造的,則最明顯。這樣,對象處於無效狀態就沒有意義了;無法創建Employee沒有id的對象。

請注意,您粘貼的例如3是而不是構造函數初始化的示例!仔細閱讀頁面,他們表示他們會在稍後介紹。你粘貼的代碼是無關的。

我會做類似如下:

class Student{ 
    final int id; 
    final String name; 

    // This is the constructor that will initialise the fields with what's passed in 
    // (Note that because the fields are final, they MUST be set by the constructor) 
    public Student(int id, String name) { 
    this.id = id; 
    this.name = name; 
    } 
} 

class TestStudent6 { 
    public static void main(String args[]){ 
    Student s1 = new Student(101, "Sonoo"); 
    System.out.println(s1.id + " " + s1.name); //printing members with a white space 
    } 
} 

(請注意,我可能也使idname領域的私人和建立getId()getName()方法,而不是訪問它們,但這是外你詢問的範圍。)

+0

對不起,我只是錯過了那條線閱讀,但如果你忘了第三個例子我的問題是非常重要的,我瞭解的基礎知識。謝謝你的解釋 – jax

0

對象有state及其內部對象,因此它應該最好從外面隱藏起來,即外部類不可訪問。將會有其他類可能依賴於對象。理想情況下,這些外部類應該依賴於公共方法來修改或讀取狀態。隱藏內部細節(狀態)並僅公開所需方法是一個好主意。

現在我們已經封裝了我們的領域,所以簡單介紹一下使用構造函數vs setter來初始化。如果一個班級由少數幾個字段組成,沒有這個類別的對象沒有意義,就像MovingCarwheels組成,所以如果有一個字段wheels那麼它必須隨着創建MovingCar的對象一起被初始化。更好的這些領域是通過構造函數創建的。其他字段,如musicSystem可能使用setter方法初始化。

有時我們可能會得到如此多的構造函數(重載),這可能會讓這樣的類的用戶感到困惑。方法是更好的,可以命名,讓用戶知道意圖。在這種情況下更好的解決辦法是builder pattern

相關問題