2012-10-10 26 views
0

我有這個問題,這是我給出的圖,我設法得到了一個解決方案,但實際問題本身是要求我實現get和set方法,我不確定它們是否是這裏適用,mutators是否適用於這裏?

enter image description here

enter image description here

這僅僅是長了一點問題的一部分,並不想使它看起來像我希望我的家庭作業由用戶完成在這裏通過粘貼整個問題,但只是一個疑問,如果get和set方法確實可以實現,我該怎麼做呢?

所以這是我的代碼,我來了這麼遠

public abstract class Property 
{ 
    private static int autonumber = 0, propID,area,price; 
    private String address; 

    public Property(String addr, int area,int price) 
    { 
     autonumber++; 
     propID = autonumber; 
     this.area = area; 
     this.price = price; 
    } 
    public abstract double calculateMiscFee(); 

    public int getArea() 
    { 
     return area; 
    } 

    public void setArea() 
    { 

    } 
//  public double calculateMiscFee() 
//  { 
//   return 100* 
//  } 
} 

這是我

public abstract class Property 
{ 
    private static int autonumber = 0; 
    private int propID; 
    private int area; 
    private double price; 
    private String address; 
    private int district; 

    public Property(String addr, int area,double price, int district) 
    { 
     autonumber++; 
     propID = autonumber; 
     this.area = area; 
     this.price = price; 
     this.district = district; 
     this.address = addr; 

    } 
    public abstract double calculateMiscFee(); 

    public int getArea() 
    { 
     return area; 
    } 

    public void setArea(int a) 
    { 
     area = a; 
    } 

     public String getAddress() 
    { 
     return address; 
    } 

    public void setAddress(String add) 
    { 
     address = add; 
    } 

    public double getPrice() 
    { 
     return price; 
    } 
    public void setPrice(double p) 
    { 
      price = p; 
    } 

    public int getDistrict() 
    { 
     return district; 
    } 

    public void setDistrict(int disc) 
    { 
      district = disc; 
    } 
//  public double calculateMiscFee() 
//  { 
//   return 100* 
//  } 

     public String toString() 
     { 

     } 

} 
+0

掃描得出的問題無法閱讀。你能編輯你的問題來清楚你所問的嗎?你只是想知道如何爲一個類實現getter和setter? –

+0

對不起,鄧肯我的辦公室代理不允許我查看圖像,所以我不知道這是不好的 – noobprogrammer

+0

你的構造函數已經設置了屬性的面積和價格。你的二傳手會做同樣的事情,但一次只能做一個。 'getArea'看起來很適合我...... –

回答

1

PROPID,面積,價格不宜staticstatic變量在一個類的所有實例之間共享,這些變量應該是每個實例,就像地址一樣。

如果一個類聲明String類型的變量abc,爲abc getter和setter將如下所示:

public String getAbc() { 
    return abc; 
} 

public void setAbc(String newAbc) { 
    /* Potentially do some validation of the new value 
    * For example make sure it's not null, or inside reasonable ranges 
    * (A price shouldn't be negative, etc.) 
    */ 
    abc = newAbc; 
} 

toString()是已經存在於所有類的方法,但你仍然可以覆蓋它。例如:

class MyClass { 
    private int myInt; 

    public MyClass(int myInt) { 
     this.myInt = myInt; 
    } 

    @Overrides 
    public String toString() { 
     return "MyClass with value " + myInt; 
    } 
} 
+0

嘿tks爲此,我已經更新了最新的工作原文,請看看。另外在這個問題中提到,必須有一個toString()方法來顯示所有的屬性如何做到這一點,我很困惑,因爲toString是一個內置的方法嗎? – noobprogrammer

+1

@noobprogrammer看我的編輯 –

0

,因爲你需要爲你的所有私人領域的getter和setter最新的代碼在這個類之外使用,比如propertyID是客戶需要的。所以你必須爲propertyID編寫getter和setter。希望這能滿足你的問題。

+0

PropertyID可能不應該是可變的。 –

+0

但propertyID是自動生成的,因爲Property類解釋它是系統生成的,你說什麼? – noobprogrammer

+0

沒有mutators for propertyid ... – Norton

0

你的屬性必須另行申報像

private static int autonumber = 0,
private static int propID = 0;
private static int area = 0;
private static int price = 0;

然後只是追加獲取或設置和利用可變的第一個字母添加的getter & setter方法。像這樣

getAutonumber
setAutonumber

最後, 如果它是一個getter,該方法應該INT不帶參數的返回。 如果它是一個setter,那麼該方法應該返回void和int的參數。

像這樣:

public static int getAutoNumber() { return autonumber; }
public static void setAutoNumber(int an) { autonumber = an; }

public static int getPropID() { return propID; }
public static void setPropID(int pi) { propID = pi; }

public static int getArea() { return area; }
public static void setArea(int a) { area = a; }

public static int getPrice() { return price; }
public static void setPrice(int p) { price = p;}

+0

如果我做了setter和getters,那麼我不需要這行代碼this.area = area;我呢? – noobprogrammer

+0

你可以,但你不想。在setter中,您仍然需要>>> this.area = area; <<<,因爲這是setter的概念,您需要通過'setter'方法'設置'變量的值。如果你刪除該行,這將是沒有用的。 –

+0

哦,明白了,大多數人似乎錯過的另一件事是,這個問題也要求區,我認爲這可以作爲可變權利實施? – noobprogrammer