2015-09-17 41 views
0

這是適當的封裝這個Java代碼?如果我省略了一些方法,但是我們需要每個方法都是必需的,我覺得它可以簡單得多。這是適當的java封裝?我很困惑

這裏的第一類:

public class Book 
{ 
private String title; 
private double price; 
private final double SALES_TAX=0.075; 

public String getTitle() 
{ 
    return title; 
} 
public void setTitle(String title) 
{ 
    this.title=title;    
} 
public double getPrice() 
{ 
    return price; 
} 
public void setPrice(double price) 
{ 
    this.price=price; 
} 
public double getSalesTax() 
{ 
    return SALES_TAX; 
} 
public double increasePrice(double incresePrice) 
{ 
    return incresePrice; 
} 
public double calculateSales(double sales) 
{ 
    return sales; 
} 
} 

而第二類:

public class BookDriver 
{ 

public static void main(String[] args) 
{ 
    Scanner keyboard=new Scanner(System.in); 

    Book bookOne=new Book(); 
    Book bookTwo=new Book(); 

    bookOne.setTitle("Life of Pi"); 
    System.out.print("Enter number to buy of "+bookOne.getTitle()+": "); 
    bookOne.setPrice(13.50*bookOne.calculateSales(keyboard.nextDouble())); 
    bookOne.setPrice((bookOne.getPrice()*bookOne.getSalesTax())+bookOne.getPrice()); 
    System.out.print("Cost for "+bookOne.getTitle()+" $"); 
    System.out.printf("%.2f"+"\n",bookOne.getPrice()); 

    bookTwo.setTitle("Harry Potter: The Goblet Of Fire"); 
    System.out.print("Enter number to buy of "+bookTwo.getTitle()+": "); 
    bookTwo.setPrice(22.00*bookTwo.calculateSales(keyboard.nextDouble())); 
    bookTwo.setPrice((bookTwo.getPrice()*bookTwo.getSalesTax())+bookTwo.getPrice()); 
    System.out.print("Cost for "+bookTwo.getTitle()+" $"); 
    System.out.printf("%.2f"+"\n",bookTwo.getPrice()); 

    System.out.print("Enter percent increase of "+bookOne.getTitle()+": "); 
    bookOne.setPrice((bookOne.getPrice()*bookOne.increasePrice(keyboard.nextDouble()))+bookOne.getPrice()); 
    System.out.printf("Cost of "+bookOne.getTitle()+": $"+"%.2f"+"\n",bookOne.getPrice()); 

    System.out.print("Enter percent increase of "+bookTwo.getTitle()+": "); 
    bookTwo.setPrice((bookTwo.getPrice()*bookTwo.increasePrice(keyboard.nextDouble()))+bookTwo.getPrice()); 
    System.out.printf("Cost of "+bookTwo.getTitle()+": $"+"%.2f"+"\n",bookTwo.getPrice()); 

    keyboard.close(); 
} 

} 

我知道這是很多,所以我沒有真正的迴應,但任何方面要求不高有助於。謝謝!!

+1

你是什麼意思「適當」? – Zarwan

+0

有人聽到字段破壞封裝(他們這樣做)。因此,在Javabeanism的時刻,他們將所有的字段都包含在getter和setter中。儘管這樣'允許改變實現'和'使用接口' - 因爲,你知道,'封裝' - 它確實沒有解決基本問題。 – user2864740

+0

我的建議是刪除不需要的setter(如果它們超出了不可變的字段;並且無論哪種情況,我都會通過構造函數獲取初始值),然後實現其他方法,以便*實際執行他們聲稱這麼做 - 要麼適當增加​​(「設定」)價格,要麼計算並返回銷售價值。 – user2864740

回答

0

讓我們看看封裝點。你有一個由屬性和方法組成的類。封裝背後的想法是,你想讓你的類中的方法成爲改變屬性值(狀態)的唯一方法。可以這樣想:如果程序中的其他代碼想要改變其中一個屬性的值,它不能自己做,它必須要求它們駐留的類有一個方法來執行它。這樣,您就可以控制對這些屬性的訪問權限。

這種方法的實現方式是使用getter和setter方法創建的。 getter方法返回屬性的值,並且setter方法將其更改爲新值。

你的getter和setter方法達到increasePrice()都不錯。您正在阻止訪問您的課程以外的其他屬性。

2.

increasePrice()只吐出了什麼傳遞給它。它不會更改任何屬性的值,因此沒有任何用處。如果你希望能夠提高價格,你可以改變的方法,像這樣:

public void increasePrice(double amountOfPriceIncrease) { 

    price += amountOfPriceIncrease; 
    /* 
    price += amountOfPriceIncrease is the same as 
    price = price + amountOfPriceIncrease 
    */ 

    } 

這行有點麻煩。對於初學者來說,increasePrice()除了吐出其中的內容之外不會做任何其他事情,其次,在一行中有很多事情要做,這使得它變得複雜和難以遵循。

bookTwo.setPrice((bookTwo.getPrice()*bookTwo.increasePrice(keyboard.nextDouble()))+bookTwo.getPrice()); 
0

你不一定需要所有的設置者。例如,它可能合理地假設一本書有一個標題,並且它不會改變。所以你可以做到最後,省略setter,並將它傳遞給構造函數。

另外,想想你是如何建模的東西。銷售稅是一本書的財產嗎?我會說不。

0

最後兩種方法沒有多大意義。你只需要返回你輸入的內容。這樣做:

public double increasePrice(double incresePrice) 
{ 
    price *= incresePrice; 
} 
public double calculateSales(double sales) 
{ 
    //return {your formula} 
}