Play! framework generates getters and setters針對模型類的每個公共字段在運行時。受益於Play中產生的獲取者和設置者!框架
public class Product {
public String name;
public Integer price;
}
將被轉化爲
public class Product {
public String name;
public Integer price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
public void setPrice(Integer price) {
this.price = price;
}
}
手動進一步解釋:
然後,當您要訪問的屬性,你可以這樣寫:
product.name = "My product";
product.price = 58;
這是翻譯在加載時間到:
product.setName("My product");
product.setPrice(58);
...並警告說:
不能直接使用getter和setter方法來訪問屬性 如果依靠自動生成。這些方法在 運行時生成。所以如果你在你編寫的代碼中引用它們,編譯器 將不會找到這些方法,並會產生一個錯誤。
因爲我不能在Play以外使用這些獲取器和設置器!項目,我看不出有什麼好處。與所有現代IDE的重構(重新封裝一個字段並更改調用者)相比,公共字段有哪些優勢?
+1好答案。我已經擴大了關於定義自己的制定者和獲取者的觀點,因爲我認爲這是一個非常重要的觀點。 – Codemwnci