Vòng đời đối tượng

Trong lập trình hướng đối tượng (OOP), vòng đời đối tượng (tiếng Anh: object lifetime hay life cycle) của một đối tượng là khoảng thời gian giữa việc tạo ra và hủy đi của đối tượng đó. Các quy tắc đối với vòng đời đối tượng có sự khác nhau đáng kể giữa các ngôn ngữ, trong một số trường hợp giữa sự hiện thực của một ngôn ngữ nhất định, và vòng đời của một đối tượng cụ thể có thể khác nhau tùy thời điểm chạy của chương trình.

Trong một số trường hợp, vòng đời của đối tượng trùng với vòng đời biến của một biến với đối tượng đó như giá trị (cho cả biến tĩnhbiến tự động), nhưng nhìn chung thì vòng đời đối tượng không được gắn với vòng đời của bất cứ biến nào. Trong nhiều trường hợp – và mặc định trong ngôn ngữ hướng đối tượng (OOL), đặc biệt với những ngôn ngữ dùng cơ chế thu gom rác (GC) – các đối tượng được cấp phát trong bộ nhớ heap, và vòng đời đối tượng không được xác định bởi vòng đời của một biến: giá trị của biến nắm giữ một đối tượng thật ra tương ứng với một tham chiếu đến đối tượng chứ không phải là đối tượng đó, và hủy đi một biến chỉ phá hủy tham chiếu chứ không phải là đối tượng bên dưới.

Ví dụ sửa

C++ sửa

class Foo
{
    // This is the prototype of the constructors
public:
    Foo(int x);
    Foo(int x, int y); // Overloaded Constructor
    Foo(const Foo &old); // Copy Constructor
    ~Foo(); // Destructor
};

Foo::Foo(int x)
{
    // This is the implementation of
    // the one-argument constructor
}

Foo::Foo(int x, int y)
{
    // This is the implementation of
    // the two-argument constructor
}

Foo::Foo(const Foo &old)
{
    // This is the implementation of
    // the copy constructor
}

Foo::~Foo()
{
    // This is the implementation of the destructor
}

int main()
{
    Foo foo(14); // call first constructor
    Foo foo2(12, 16); // call overloaded constructor
    Foo foo3(foo); // call the copy constructo
 
    return 0;
    // destructors called in backwards-order
    // here, automatically
}

Java sửa

class Foo
{
    public Foo(int x)
    {
        // This is the implementation of
        // the one-argument constructor
    }

    public Foo(int x, int y)
    {
        // This is the implementation of
        // the two-argument constructor
    }

    public Foo(Foo old)
    {
        // This is the implementation of
        // the copy constructor
    }

    public static void main(String[] args)
    {
        Foo foo = new Foo(14); // call first constructor
        Foo foo2 = new Foo(12, 16); // call overloaded constructor
        Foo foo3 = new Foo(foo); // call the copy constructor
        // garbage collection happens under the covers, and objects are destroyed
    }
}

C# sửa

namespace ObjectLifeTime 
{
class Foo
{
    public Foo()
    {
        // This is the implementation of
        // default constructor
    }

    public Foo(int x)
    {
        // This is the implementation of
        // the one-argument constructor
    }
     ~Foo()
    {
        // This is the implementation of
        // the destructor
    }

    public Foo(int x, int y)
    {
        // This is the implementation of
        // the two-argument constructor
    }
 
    public Foo(Foo old)
    {
        // This is the implementation of
        // the copy constructor
    }
 
    public static void Main(string[] args)
    {
        Foo defaultfoo = new Foo(); // call default constructor
        Foo foo = new Foo(14); // call first constructor
        Foo foo2 = new Foo(12, 16); // call overloaded constructor
        Foo foo3 = new Foo(foo); // call the copy constructor
       
    }
 }
}

Objective-C sửa

#import <objc/Object.h>

@interface Point: Object
{
   double x;
   double y;
}

//These are the class methods; we have declared two constructors
+ (Point *) newWithX: (double) andY: (double);
+ (Point *) newWithR: (double) andTheta: (double);

//Instance methods
- (Point *) setFirstCoord: (double);
- (Point *) setSecondCoord: (double);

/* Since Point is a subclass of the generic Object 
 * class, we already gain generic allocation and initialization
 * methods, +alloc and -init. For our specific constructors
 * we can make these from these methods we have
 * inherited.
 */
@end
 
@implementation Point

- (Point *) setFirstCoord: (double) new_val
{
   x = new_val;
}

- (Point *) setSecondCoord: (double) new_val
{
   y = new_val;
}

+ (Point *) newWithX: (double) x_val andY: (double) y_val
{
   //Concisely written class method to automatically allocate and 
   //perform specific initialization.
   return [[[Point alloc] setFirstCoord:x_val] setSecondCoord:y_val]; 
}

+ (Point *) newWithR: (double) r_val andTheta: (double) theta_val
{
   //Instead of performing the same as the above, we can underhandedly
   //use the same result of the previous method
   return [Point newWithX:r_val andY:theta_val];
}

@end

int
main(void)
{
   //Constructs two points, p and q.
   Point *p = [Point newWithX:4.0 andY:5.0];
   Point *q = [Point newWithR:1.0 andTheta:2.28];

   //...program text....
   
   //We're finished with p, say, so, free it.
   //If p allocates more memory for itself, may need to
   //override Object's free method in order to recursively
   //free p's memory. But this is not the case, so we can just
   [p free];

   //...more text...

   [q free];

   return 0;
}

Object Pascal sửa

program Example;

type

  DimensionEnum =
    (
      deUnassigned,
      de2D,
      de3D,
      de4D
);

  PointClass = class
  private
    Dimension: DimensionEnum;

  public
    X: Integer;
    Y: Integer;
    Z: Integer;
    T: Integer;

  public
    (* prototype of constructors *)

    constructor Create();
    constructor Create(AX, AY: Integer);
    constructor Create(AX, AY, AZ: Integer);
    constructor Create(AX, AY, AZ, ATime: Integer);
    constructor CreateCopy(APoint: PointClass);

    (* prototype of destructors *)

    destructor Destroy;
  end;

constructor PointClass.Create();
begin
  // implementation of a generic, non argument constructor
  Self.Dimension:= deUnassigned;
end;

constructor PointClass.Create(AX, AY: Integer);
begin
  // implementation of a, 2 argument constructor
  Self.X:= AX;
  Y:= AY;

  Self.Dimension:= de2D;
end;

constructor PointClass.Create(AX, AY, AZ: Integer);
begin
  // implementation of a, 3 argument constructor
  Self.X:= AX;
  Y:= AY;
  Self.X:= AZ;

  Self.Dimension:= de3D;
end;

constructor PointClass.Create(AX, AY, AZ, ATime: Integer);
begin
  // implementation of a, 4 argument constructor
  Self.X:= AX;
  Y:= AY;
  Self.X:= AZ;
  T:= ATime;

  Self.Dimension:= de4D;
end;

constructor PointClass.CreateCopy(APoint: PointClass);
begin
  // implementation of a, "copy" constructor
  APoint.X:= AX;
  APoint.Y:= AY;
  APoint.X:= AZ;
  APoint.T:= ATime;

  Self.Dimension:= de4D;
end;

destructor PointClass.PointClass.Destroy;
begin
  // implementation of a generic, non argument destructor
  Self.Dimension:= deUnAssigned;
end;

var
  (* variable for static allocation *)
  S:  PointClass;
  (* variable for dynamic allocation *)
  D: ^PointClass;

begin (* of program *)
  (* object lifeline with static allocation *)
  S.Create(5, 7);

  (* do something with "S" *)

  S.Destroy;

  (* object lifeline with dynamic allocation *)
  D = new PointClass, Create(5, 7);

  (* do something with "D" *)

  dispose D, Destroy;
end.  (* of program *)

Python sửa

class Socket:
    def __init__(self, remote_host):
        self.connection = connectTo(remote_host)

    def send(self):
        # send data

    def recv(self):
        # receive data;

def f():
    s = Socket('example.com')
    s.send('test')
    return s.recv()

Ghi chú sửa

Tham khảo sửa