Posted on 2011-05-31 11:21
zuora 閱讀(90)
評論(0) 編輯 收藏
Field與property都屬于面向對象方法學中的屬于——“狀態”,與“行為”相對。
Field 又稱為域,instance level
field又稱為成員變量;class
level field又稱為靜態變量;
Property 又稱為屬性,instance level稱為成員屬性,class level
properties稱為靜態屬性。
Note: Apex Using Static Properties
When a property is declared as
static, the property's accessor methods execute in a static context. This means
that the accessors do not have access to non-static member variables defined in
the class.
在Java, Action Script或者Apex編程語言中,屬性(Property)會以不同樣式的Getter或Setter形式存在。
如在Java中,Property定義方式遵照JavaBean的命名規范如下:
public int getWidth() {
return this.measuredWidth;
}
public void setWidth(int width) {
this.explictWidth = width;
invalidteProperties();
} |
在Action
Script中,Property可以定義如下:
public function get width():Integer {
return this.measuredWidth;
}
public function set width(var width:Integer):void {
this.explictWidth = width;
invalidteProperties();
} |
在Apex中,Property定義如下:
public Integer width {
get {
return this.measuredWidth;
}
set {
this.explictWidth = width;
invalidteProperties();
}
} |
在Java編程中對屬性值的設置,通常通過調用Setter完成,如下:
...
object.setWidth(10);
...
|
而在Action Script或者Apex編程語言中,可以通過對屬性名稱直接賦值完成,如下:
上述一次賦值操作將導致調用一次width的setter方法。
給出Property的Sample Code是為了讓初學者更清楚Field與Property的區別與聯系。