[OCAJP]電費帳單

Given the following two classes:

How should you write methods in the ElectricAccount class at line n1 so that the member variable bill is always equal to the value of the member variable kwh multiplied by the member variable rate?

Any amount of electricity used by a customer (represented by an instance of the customer class) must contribute to the customer's bill (represented by the member variable bill) through the method useElectricity method. An instance of the customer class should never be able to tamper with or decrease the value of the member variable bill.

A.

B.

C.

D.

題解

選項A,沒有判斷傳入的kWh參數的值是否大於0,因此可能會有傳入負數的情形。

選項B,正確答案。

選項C,使用private來修飾addKWh方法,會導致ElectricAccount之外的其他類別無法存取。

選項D,setBill方法應該要改成private來修飾,避免帳單被竄改。

關於作者

Magic Len

各位好,我是Magic Len,是這網站的管理員。我是台灣台中大肚山上人,畢業於台中高工資訊科和台灣科技大學資訊工程系,曾在桃機航警局服役。我熱愛自然也熱愛科學,喜歡和別人分享自己的知識與經驗。如果你有興趣認識我,可以加我的Facebook,並且請註明是從MagicLen來的。

相關文章