牛肉饺子馅配什么蔬菜好吃| leu是什么氨基酸| 睾丸炎用什么药| 糖化高是什么意思| 草字头内念什么| 童子是什么意思| 腰酸胀是什么原因| facebook是什么| 骨髓瘤是什么病| 吃饭肚子疼是什么原因| 现在去贵州穿什么衣服| 张五行属什么| 炖牛骨头放什么调料| 耳刀旁加步念什么| 红参有什么作用| 尿糖阳性是什么意思| 梦见挖红薯是什么意思| 大暑是什么时候| 翊字五行属什么| 口水多是什么原因引起的| 经常早上肚子疼是什么原因| 农垦局是什么性质单位| 可尔必思是什么饮料| 当今社会什么行业前途比较好| 膝盖小腿酸软无力是什么原因| 脂肪肝是什么病| 属猴女和什么属相最配| 毓婷和金毓婷有什么区别| 钦点是什么意思| 百白破是预防什么的| 吃天麻对身体有什么好处| 上海有什么好玩的地方适合小孩子| 心脏上有个小洞是什么病| 手麻去医院挂什么科| 咳嗽喝什么药| 奥斯卡小金人什么意思| 中国国粹是什么| 鸟衣念什么| 做梦梦到钱是什么预兆| 吃惊的什么| 什么食物是养肝的| 柔顺和拉直有什么区别| 什么样的人容易得甲减| 引什么大叫| 乳腺导管扩张是什么意思严重吗| 胎动什么感觉| 肌张力高是什么意思| 狼吞虎咽的意思是什么| 投桃报李是什么生肖| 最近有什么病毒| 熬夜伤什么| 自己家院子种什么树好| 牛筋面是什么做的| 前戏是什么意思| 盆腔炎检查什么项目| KTV服务员主要做什么| 最高的山是什么山| 什么季节最短| 女性性高潮是什么感觉| 高血压吃什么菜| tp什么意思| 常务副县长什么级别| 亚瑟士和鬼冢虎的区别是什么| 癌胚抗原是什么意思| 脚痒是什么原因| 什么水果含糖量高| tu是什么意思| 膀胱炎是什么症状表现| 谁与争锋是什么意思| 大惊小怪是什么意思| 白带多是为什么| 为什么会便秘| 铁剂不能和什么一起吃| 经期吃什么让血量增加| 单核细胞是什么意思| 什么是高利贷| 闪购是什么| 三分三是什么药| 什么是直肠炎| 割包皮挂什么科室| 乙状结肠管状腺瘤是什么意思| 突然头晕冒虚汗什么原因| 镭射有什么危害| 牛奶什么时候喝最好| 80年属猴的是什么命| 血小板上升是什么原因| 什么邮票最值钱| 乏力是什么意思| 水痘通过什么途径传染| 全性向是什么意思| 牛和什么生肖最配| 三个水读什么| 法院是什么机关| 双肺纹理增多什么意思| 缓解是什么意思| 四十不惑是什么意思| 智商130算什么水平| 什么是八字生辰八字| 小狗是什么意思| 痔疮是什么病| 青衣是什么意思| 高中什么时候分文理科| 麦高芬是什么意思| 什么是癣| 肛周湿疹用什么药膏效果好| 脚拇指外翻是什么原因造成的| 十余载是什么意思| 日昳是什么意思| 丙氨酸氨基转移酶是什么意思| 篦子是什么东西| 香米是什么米| 左侧卵巢无回声是什么意思| 芊芊是什么颜色| ofs是什么意思| 美国为什么那么强大| 梦见蛇缠身是什么意思| 白带有血丝是什么原因| 7月16日什么星座| 鸡肠炎用什么药效果好| 严什么什么重| 鸡奸是什么意思| 银行卡户名是什么意思| 重日是什么意思| 竹心泡水喝有什么功效| iga是什么| 利字五行属什么| np文是什么意思| 嘴上有痣代表什么| 糖料病者应吃什么好| 天龙八部是指佛教中的什么| 七月十四号是什么星座| 非典型腺细胞是什么意思| 天蝎属于什么象星座| 什么是邪教| 清油是什么油| 夜尿增多是什么原因| 沉香有什么作用| sop是什么意思| 为什么尿液一直是黄的| 经期适合喝什么汤| 什么潭什么穴| 散光跟近视有什么区别| 嘴唇紫色是什么原因| 打呼噜去医院挂什么科| 支气管炎改变什么意思| md是什么学位| 过年吃什么| 非甾体抗炎药是什么意思| 内分泌紊乱吃什么药| 张国立的老婆叫什么名字| 枇杷什么味道| 腺肌症有什么症状表现| 晨勃是什么意思| 麒麟长什么样| 老来得子是什么意思| 318是什么日子| h代表什么| 上大号出血是什么原因| 百合是什么颜色| 毛囊炎什么症状| 格力空调睡眠模式1234什么意思| 人生最大的遗憾是什么| 回潮是什么意思| gccg是什么牌子| 什么食物含碘| 包干价是什么意思| 血糖高吃什么能降糖| 头发轻轻一拉就掉了是什么原因| 裙带菜是什么| 肝郁气滞吃什么药| 脸上长扁平疣是什么原因引起的| 熬夜伤什么| spo2过低是什么意思| 大什么什么针| 支配是什么意思| 哮喘吃什么药好| 眼白有黄斑是什么原因| 臻字的意思是什么| 什么狗聪明听话又好养| 争强好胜什么意思| 白带是什么颜色的| 痛风吃什么消炎药| 未央是什么意思| 饭后打嗝是什么原因| 介怀是什么意思| 右鼻子经常出血是什么原因| 羊排和什么一起炖好吃| 雾化治疗的作用是什么| 手红是什么原因| 什么样的青蛙| 鳄鱼的天敌是什么动物| 开水烫伤用什么药膏好得快| 后循环缺血是什么意思| 老人流口水是什么原因引起的| 有缘无份什么意思| 甲亢能吃什么水果| 生粉是什么粉| 怀孕初期需要注意些什么| 眼睛模糊吃什么好| 小三阳吃什么食物好得快| 吃奇亚籽有什么好处| 乔迁之喜送什么| 感冒了吃什么水果比较好| 来龙去脉是什么生肖| 心脏支架不能吃什么| 北京是我国的什么中心| 狐假虎威什么意思| 13太保是什么意思| 下巴长痘痘用什么药| 怀孕不到一个月有什么症状| 双手抱在胸前代表什么| 扁桃体发炎是什么引起的| 姝字五行属什么| 什么的果实| 乳头疼吃什么药| 什么回大什么| 静脉曲张挂什么科室| 什么水果有助于减肥| 什么时候开始孕吐| 眼角疼是什么原因| 坐班什么意思| 艾滋病潜伏期有什么症状| 表头是什么| 党什么时候成立| 兔子尾巴像什么| 辛苦是什么意思| 态度是什么意思| 程门立雪是什么意思| 制剂是什么意思| 闭锁是什么意思| 浑身疼吃什么药| 迥异是什么意思| 6月23号是什么日子| 做梦抓到很多鱼是什么征兆| 天蝎配什么星座| 菠萝是什么意思| 农历闰月有什么规律| 稀松平常是什么意思| 孕妇梦见大蟒蛇是什么意思| 高血压用什么药| 宫颈纳囊用什么药治疗效果好| 鹅蛋不能和什么一起吃| 什么是包皮| 匙仁是牛的什么部位| 佳字属于五行属什么| 牛油果对身体有什么好处| package什么意思| 什么心什么意| 卡介苗什么时候接种| 勤劳的小蜜蜂什么意思| 两三分钟就射什么原因| 哭笑不得是什么意思| 面粉和淀粉有什么区别| 血糖看什么指标| 什么木做菜板最好| 身心合一是什么意思| 永加日念什么| 吃什么能排结石| 黑话是什么意思| 6月初三是什么日子| 一如既往的意思是什么| ins是什么| 啄木鸟为什么不会脑震荡| 血压偏低有什么症状| 百度
百度 那张被人津津乐道的照片,照片最左侧挂着相机的男子一度被错认为普京。

A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) translated to 'getter' and 'setter' method calls. The field-like syntax is easier to read and write than many method calls,[citation needed] yet the interposition of method calls "under the hood" allows for data validation, active updating (e.g., of GUI elements), or implementation of what may be called "read-only fields".

Support in languages

edit

Programming languages that support properties include ActionScript 3, C#, D, Delphi/Free Pascal, eC, F#, Kotlin, JavaScript, Objective-C 2.0, Python, Scala, Swift, Lua, and Visual Basic.

Some object-oriented languages, such as Java and C++, do not support properties, requiring the programmer to define a pair of accessor and mutator methods instead.[1][citation needed]

Oberon-2 provides an alternative mechanism using object variable visibility flags.[citation needed]

Other languages designed for the Java Virtual Machine, such as Groovy, natively support properties.

While C++ does not have first class properties, they can be emulated with operator overloading.[2]

Also note that some C++ compilers support first class properties as language extensions.[citation needed]

In many object oriented languages properties are implemented as a pair of accessor/mutator methods, but accessed using the same syntax as for public fields. Omitting a method from the pair yields a read-only or an uncommon write-only property.

In some languages with no built-in support for properties, a similar construct can be implemented as a single method that either returns or changes the underlying data, depending on the context of its invocation. Such techniques are used e.g. in Perl. [citation needed]

Some languages (Ruby, Smalltalk) achieve property-like syntax using normal methods, sometimes with a limited amount of syntactic sugar.

Syntax variants

edit

Some languages follow well-established syntax conventions for formally specifying and utilizing properties and methods.

Among these conventions:

  • Dot notation
  • Bracket notation

Dot notation

edit

The following example demonstrates dot notation in JavaScript.

document.createElement('pre');

Bracket notation

edit

The following example demonstrates bracket notation in JavaScript.

document['createElement']('pre');

Example syntax

edit
class Pen 
{
    private int color; // private field
    
    // public property
    public int Color 
    {  
        get
        {
            return this.color;
        }
        set 
        {
            if (value > 0) {
                this.color = value;
            }
        }
    }
}
// accessing:
Pen pen = new Pen();
int color_tmp = 0;
// ...
pen.Color = 17;
color_tmp = pen.Color;
// ...
pen.Color = ~pen.Color; // bitwise complement ...

// another silly example:
pen.Color += 1; // a lot clearer than "pen.set_Color(pen.get_Color() + 1)"!

Recent C# versions also allow "auto-implemented properties" where the backing field for the property is generated by the compiler during compilation. This means that the property must have a setter. However, it can be private.

class Shape 
{
    public int Height { get; set; }
    public int Width { get; private set; }
}

C++ does not have first class properties, but there exist several ways to emulate properties to a limited degree. Two of which follow:

Using Standard C++

edit
#include <iostream>

template <typename T> class property {
        T value;
    public:
        T & operator = (const T &i) {
            return value = i;
        }
        // This template class member function template serves the purpose to make
        // typing more strict. Assignment to this is only possible with exact identical types.
        // The reason why it will cause an error is temporary variable created while implicit type conversion in reference initialization.
        template <typename T2> T2 & operator = (const T2 &i) {
            T2 &guard = value;
            throw guard; // Never reached.
        }

        // Implicit conversion back to T. 
        operator T const & () const {
            return value;
        }
};

struct Foo {
    // Properties using unnamed classes.
    class {
            int value;
        public:
            int & operator = (const int &i) { return value = i; }
            operator int () const { return value; }
    } alpha;

    class {
            float value;
        public:
            float & operator = (const float &f) { return value = f; }
            operator float () const { return value; }
    } bravo;
};

struct Bar {
    // Using the property<>-template.
    property <bool> alpha;
    property <unsigned int> bravo;
};

int main () {
    Foo foo;
    foo.alpha = 5;
    foo.bravo = 5.132f;

    Bar bar;
    bar.alpha = true;
    bar.bravo = true; // This line will yield a compile time error
                      // due to the guard template member function.
    ::std::cout << foo.alpha << ", "
                << foo.bravo << ", "
                << bar.alpha << ", "
                << bar.bravo
                << ::std::endl;
    return 0;
}

Also see Stack Overflow for a more detailed example.

C++, Microsoft, GCC, LLVM/clang and C++Builder-specific

edit

An example taken from the MSDN documentation page.

// declspec_property.cpp
struct S
{
   int i;
   void putprop(int j)
   { 
      i = j;
   }

   int getprop()
   {
      return i;
   }

   __declspec(property(get = getprop, put = putprop)) int the_prop;
};

int main()
{
   S s;
   s.the_prop = 5;
   return s.the_prop;
}
class Pen
{
    private int m_color; // private field
    
    // public get property
    public int color () {
        return m_color;
    }
    
    // public set property
    public void color (int value) {
         m_color = value;
    }
}
auto pen = new Pen;
pen.color = ~pen.color; // bitwise complement

// the set property can also be used in expressions, just like regular assignment
int theColor = (pen.color = 0xFF0000);

In D version 2, each property accessor or mutator must be marked with @property:

class Pen
{
    private int m_color; // private field
    
    // public get property
    @property public int color () {
        return m_color;
    }
    
    // public set property
    @property public void color (int value) {
        m_color = value;
    }
}

Delphi/Free Pascal

edit
type TPen = class
  private
    FColor: TColor;
    function GetColor: TColor;
    procedure SetColor(const AValue: TColor);
  public
    property Color: Integer read GetColor write SetColor;
end;

function TPen.GetColor: TColor;
begin
  Result := FColor;
end;

procedure TPen.SetColor(const AValue: TColor);
begin
  if FColor <> AValue
   then FColor := AValue;
end;
// accessing:
var Pen: TPen;
// ...
Pen.Color := not Pen.Color;

(*
Delphi and Free Pascal also support a 'direct field' syntax -

property Color: TColor read FColor write SetColor;

or

property Color: TColor read GetColor write FColor;

where the compiler generates the exact same code as for reading and writing
a field. This offers the efficiency of a field, with the safety of a property.
(You can't get a pointer to the property, and you can always replace the member
access with a method call.)
*)
class Pen 
{
   // private data member
   Color color;
public:
   // public property
   property Color color 
   {  
      get { return color; }
      set { color = value; }
   }
}
Pen blackPen { color = black };
Pen whitePen { color = white };
Pen pen3 { color = { 30, 80, 120 } };
Pen pen4 { color = ColorHSV { 90, 20, 40 } };
type Pen() = class
    let mutable _color = 0

    member this.Color
        with get() = _color
        and set value = _color <- value
end
let pen = new Pen()
pen.Color <- ~~~pen.Color

JavaScript

edit
function Pen() {
    this._color = 0;
}
// Add the property to the Pen type itself, can also
// be set on the instance individually
Object.defineProperties(Pen.prototype, {
    color: {
        get: function () {
            return this._color;
        },
        set: function (value) {
            this._color = value;
        }
    }
});
var pen = new Pen();
pen.color = ~pen.color; // bitwise complement
pen.color += 1; // Add one

ActionScript 3.0

edit
package {
	public class Pen {
		private var _color:uint = 0;
		
		public function get color ():uint {
			return _color;
		}
		
		public function set color(value:uint):void {
			_color = value;
		}
	}
}
var pen:Pen = new Pen();
pen.color = ~pen.color; // bitwise complement
pen.color += 1; // add one

Objective-C 2.0

edit
@interface Pen : NSObject
@property (copy) NSColor *colour;	// The "copy" attribute causes the object's copy to be
					// retained, instead of the original.
@end

@implementation Pen
@synthesize colour;			// Compiler directive to synthesise accessor methods.
					// It can be left behind in Xcode 4.5 and later.
@end

The above example could be used in an arbitrary method like this:

Pen *pen = [[Pen alloc] init];
pen.colour = [NSColor blackColor];
float red = pen.colour.redComponent;
[pen.colour drawSwatchInRect: NSMakeRect(0, 0, 100, 100)];
class Pen
{
    private int $color = 1;

    function __set($property, $value)
    {
        if (property_exists($this, $property)) { 
            $this->$property = $value;
        }
    }

    function __get($property)
    {
        if (property_exists($this, $property)) {
            return $this->$property;
        }
        return null;
    }
}
$p = new Pen();
$p->color = ~$p->color; // Bitwise complement
echo $p->color;

Python

edit

Properties only work correctly for new-style classes (classes that have object as a superclass), and are only available in Python 2.2 and newer (see the relevant section of the tutorial Unifying types and classes in Python 2.2). Python 2.6 added a new syntax involving decorators for defining properties.

class Pen:
    def __init__(self) -> None:
        self._color = 0  # "private" variable

    @property
    def color(self):
        return self._color

    @color.setter
    def color(self, color):
        self._color = color
pen = Pen()
# Accessing:
pen.color = ~pen.color  # Bitwise complement ...

Ruby

edit
class Pen
  def initialize
    @color = 0
  end
    
  # Defines a getter for the @color field
  def color
    @color
  end

  # Defines a setter for the @color field
  def color=(value)
    @color = value
  end
end

pen = Pen.new
pen.color = ~pen.color    # Bitwise complement

Ruby also provides automatic getter/setter synthesizers defined as instance methods of Class.

class Pen
  attr_reader :brand    # Generates a getter for @brand (Read-Only)
  attr_writer :size     # Generates a setter for @size  (Write-Only)
  attr_accessor :color  # Generates both a getter and setter for @color (Read/Write)

  def initialize
    @color = 0          # Within the object, we can access the instance variable directly
    @brand = "Penbrand"
    @size = 0.7         # But we could also use the setter method defined by the attr_accessor Class instance method
  end
end

pen = Pen.new
puts pen.brand           # Accesses the pen brand through the generated getter
pen.size = 0.5           # Updates the size field of the pen through the generated setter
pen.color = ~pen.color

Visual Basic

edit

Visual Basic (.NET 2003–2010)

edit
Public Class Pen
 
    Private _color As Integer ' Private field

    Public Property Color() As Integer ' Public property
        Get
            Return _color
        End Get
        Set(ByVal value As Integer)
            _color = value
        End Set
    End Property

End Class
' Create Pen class instance
Dim pen As New Pen()

' Set value
pen.Color = 1

' Get value
Dim color As Int32 = pen.Color

Visual Basic (only .NET 2010)

edit
Public Class Pen

    Public Property Color() As Integer ' Public property

End Class
' Create Pen class instance
Dim pen As New Pen()

' Set value
pen.Color = 1

' Get value
Dim color As Int32 = pen.Color

Visual Basic 6

edit
' in a class named clsPen
Private m_Color As Long

Public Property Get Color() As Long
    Color = m_Color
End Property

Public Property Let Color(ByVal RHS As Long)
    m_Color = RHS
End Property
' accessing:
Dim pen As New clsPen
' ...
pen.Color = Not pen.Color

See also

edit

References

edit
  1. ^ "Accessors And Mutators In Java". C# Corner - Community of Software and Data Developers. Retrieved 5 January 2022.
  2. ^ "Portability of Native C++ properties". Stack Overflow. Stack Overflow. Retrieved 5 January 2022.
  3. ^ "property (C++)". Microsoft technical documentation. Microsoft. Retrieved 5 January 2022.
  4. ^ "clang::MSPropertyDecl Class Reference". Clang: a C language family frontend for LLVM. Retrieved 5 January 2022.
  5. ^ "__property Keyword Extension". Embarcadero/IDERA Documentation Wiki. Retrieved 5 January 2022.
玫瑰糠疹用什么药 家里放什么最招财 apc是什么 铠字五行属什么 rsa是什么意思
慕名而来是什么意思 维生素c吃多了有什么危害 两边太阳胀痛什么原因引起的 手指指尖发麻是什么原因 什么降血脂效果最好的
去香港买什么划算 白事的礼金叫什么 为什么过敏反复发作 梦到伟人有什么兆头 借你吉言是什么意思
放荡不羁爱自由什么意思 一见倾心什么意思 edsheeran为什么叫黄老板 脚心痛什么原因 肠炎吃什么药效果好
6.4是什么星座hcv9jop0ns5r.cn 低血压是什么症状hcv8jop2ns4r.cn 牙龈发炎肿痛吃什么药dayuxmw.com 重阳节吃什么hcv7jop9ns0r.cn 大熊猫的尾巴是什么颜色hcv8jop3ns5r.cn
八婆是什么意思hcv7jop4ns5r.cn 摩羯座的幸运花是什么mmeoe.com 吐气如兰是什么意思hcv8jop5ns1r.cn 芝五行属什么hcv9jop7ns2r.cn 什么去疤痕效果最好hcv9jop2ns6r.cn
什么血型生出o型血hcv7jop9ns0r.cn 尿毒症是什么hcv7jop6ns5r.cn 眼睛老是流眼泪是什么原因hcv8jop3ns1r.cn 淋巴结有血流信号预示着什么hcv7jop9ns0r.cn rich什么意思hcv9jop5ns6r.cn
小孩发烧吃什么药hcv8jop7ns6r.cn 女人得性瘾什么症状hcv9jop0ns5r.cn 轭是什么意思xjhesheng.com 大腿为什么会长妊娠纹hcv9jop6ns6r.cn 对酒当歌是什么生肖hcv7jop6ns7r.cn
百度