Đặc tính (lập trình)

Đặc tính (tiếng Anh: property), trong một số ngôn ngữ lập trình hướng đối tượng, là một loại đặc biệt của thành viên lớp, trung gian chức năng giữa một trường (hay thành viên dữ liệu) và một phương thức. Cú pháp để đọc và viết các đặc tính thì giống như của trường, nhưng thường được chuyển thành lời gọi phương thức 'getter' và 'setter'. Cú pháp giống như trường khiến nó dễ đọc và viết hơn nhiều so với việc gọi phương thức, và xen lẫn bên trong giữa gọi phương thức cho phép xác nhận dữ liệu, cập nhật chủ động (ví dụ như, cho các phần tử của giao diện đồ họa người dùng), hay để hiện thực của các trường "chỉ đọc" (read-only).

Xem ví dụ về bài học cho ngôn ngữ C # bên dưới.

Hỗ trợ trong các ngôn ngữ sửa

Các ngôn ngữ lập trình hỗ trợ đặc tính bao gồm ActionScript 3, C#, D, Delphi/Free Pascal, eC, F#, Kotlin, JavaScript, Objective-C 2.0, Python, Scala, Swift, Lua, và Visual Basic. Một vài ngôn ngữ lập trình hướng đối tượng, như JavaC++, không hỗ trợ đặc tính, mà yêu cầu lập trình viên định nghĩa một cặp phương thức truy cậpthay đổi.

Ví dụ cú pháp sửa

C# sửa

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)"!

C++ sửa

C++ không có đặc tính lớp đầu tiên, nhưng có vài cách để mô phỏng đặc tính ở một mức độ giới hạn. Hai trong số đó là:

#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.        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;}

C++, Microsoft & C++Builder sửa

Ví dụ này được lấy từ MSDN Trang tài liệu.

// declspec_property.cppstruct 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;}

D sửa

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 assignmentint theColor = (pen.color = 0xFF0000);

Ở phiên bản thứ hai của D, mỗi phương thức truy cập và thay đổi phải được đánh dấu với @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 sửa

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 also supports a 'direct field' syntax -property Color: TColor read FColor write SetColor;orproperty Color: TColor read GetColor write FColor;where the compiler generates the exact same code as for reading and writinga 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 memberaccess with a method call.)*)

eC sửa

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 } };

F# sửa

type Pen() = class    let mutable _color = 0    member this.Color        with get() = _color        and set value = _color <- valueend
let pen = new Pen()pen.Color <- ~~~pen.Color

JavaScript sửa

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

ActionScript 3.0 sửa

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 complementpen.color += 1; // add one

Objective-C 2.0 sửa

@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

Ví dụ trên có thể được sử dụng trong một phương thức tùy ý như vầy:

Pen *pen = [[Pen alloc] init];pen.colour = [NSColor blackColor];float red = pen.colour.redComponent;[pen.colour drawSwatchInRect: NSMakeRect(0, 0, 100, 100)];

PHP sửa

class Pen {    private $_color;    function __set($property, $value) {        if ($property == 'Color') {             return $this->_color = $value;        }    }    function __get($property) {        if ($property == 'Color') {            return $this->_color;        }    }}
$p = new Pen();$p->Color = ~$p->Color; // bitwise complementecho $p->Color;

Python sửa

class Pen(object):    def __init__(self):        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 sửa

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    endendpen = Pen.newpen.color = ~pen.color    # Bitwise complement

Visual Basic sửa

Visual Basic (.NET 2003-2010) sửa

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 PropertyEnd Class
' Create Pen class instanceDim pen As New Pen()' Set valuepen.Color = 1' Get valueDim color As Int32 = pen.Color

Visual Basic (chỉ.NET 2010) sửa

Public Class Pen    Public Property Color() As Integer ' Public propertyEnd Class
' Create Pen class instanceDim pen As New Pen()' Set valuepen.Color = 1' Get valueDim color As Int32 = pen.Color

Visual Basic 6 sửa

' in a class named clsPenPrivate m_Color As LongPublic Property Get Color() As Long    Color = m_ColorEnd PropertyPublic Property Let Color(ByVal RHS As Long)    m_Color = RHSEnd Property
' accessing:Dim pen As New clsPen'...pen.Color = Not pen.Color

Xem thêm sửa

Tham khảo sửa

🔥 Top keywords: 2112: Doraemon ra đời300 (phim)Anh hùng xạ điêu (phim truyền hình 2003)Bùng phát virus Zika 2015–2016Chuyên gia trang điểmCristiano RonaldoCá đuối quỷDanh sách Tổng thống Hoa KỳDanh sách câu thần chú trong Harry PotterDanh sách tài khoản Instagram có nhiều lượt theo dõi nhấtGiải Oscar cho phim ngắn hay nhấtHoan Ngu Ảnh ThịHầu tướcHọc thuyết tế bàoJason Miller (communications strategist)Lễ hội Chọi trâu Đồ SơnLộc Đỉnh ký (phim 1998)Natapohn TameeruksNinh (họ)Phim truyền hình Đài LoanRobloxThanh thiếu niênThần tượng teenThổ thần tập sựTrang ChínhTập hợp rỗngTỉnh của Thổ Nhĩ KỳVõ Thần Triệu Tử LongXXX (loạt phim)Âu Dương Chấn HoaĐào Trọng ThiĐại học Công giáo ParisĐệ Tứ Cộng hòa PhápĐổng Tiểu UyểnĐài Truyền hình Kỹ thuật số VTCLương Tam QuangTrang ChínhTô LâmTrần Quốc TỏThích Minh TuệĐặc biệt:Tìm kiếmLê Thành LongBộ trưởng Bộ Công an (Việt Nam)Nguyễn Duy NgọcBộ Công an (Việt Nam)Vòng loại Giải vô địch bóng đá thế giới 2026 – Khu vực châu ÁLương CườngNguyễn Thị Thanh (chính khách)Thích Chân QuangViệt NamTô Ân XôCleopatra VIIĐài Truyền hình Việt NamPhạm Minh ChínhBảng xếp hạng bóng đá nam FIFANguyễn Văn Long (Sĩ quan)Giải vô địch bóng đá châu Âu 2024LGBTĐội tuyển bóng đá quốc gia Việt NamBộ Chính trị Ban Chấp hành Trung ương Đảng Cộng sản Việt NamKim Sang-sikTiếp sức mùa thiThứ trưởng Bộ Công an (Việt Nam)Minh Đăng QuangChủ tịch nước Cộng hòa xã hội chủ nghĩa Việt NamLê Quốc HùngPhan Đình TrạcNguyễn Phú TrọngBi sắtKylian MbappéPhạm Thế TùngTrần Đại QuangMưa sao băng