有什麼更好的方式與private field
和private methods
工作過他們嗎?私人領域的公共方法
是它優美的使用私有方法的內部私有字段或最好是把他們作爲參數,從公開的方法,當調用私有方法?
領域:
private Item model;
公共方法:
...
if (model.getPrice() != null) {
String formattedPrice = formatPrice();
}
...
私有方法:
private int formatPrice(){
int price = model.getPrice() + 10;
}
VS
2. 公共方法:
if (model.getPrice() != null) {
String formattedPrice = formatPrice(model.getPrice());
}
...
私有方法:
formatPrice(int price){
int price = price + 10;
}
如果你的'modifyPrice()'方法只能在這個類中使用(這似乎是因爲'private'的可見性),我更喜歡第一個答案。 – sp00m 2012-04-24 13:00:31
感謝您的建議,我也使用第一,但猶豫 – sergionni 2012-04-24 13:02:14
@sergionni所以,不要猶豫;) – 2012-04-24 13:08:31