Thực đơn
Vòng_đời_đối_tượng Ví dụclass Foo{ // This is the prototype of the constructorspublic: 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}
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 }}
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 } }}
#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];}@endintmain(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;}
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 *)
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()
Thực đơn
Vòng_đời_đối_tượng Ví dụLiên quan
Vòng loại giải vô địch bóng đá châu Âu 2024 Vòng loại Giải vô địch bóng đá thế giới 2026 – Khu vực châu Á Vòng loại giải vô địch bóng đá châu Âu 2024 (vòng play-off) Vòng loại giải vô địch bóng đá châu Âu 2020 Vòng loại Giải vô địch bóng đá thế giới 2026 Vòng loại Giải vô địch bóng đá thế giới 2022 Vòng loại Giải vô địch bóng đá thế giới 2022 – Khu vực châu Âu Vòng loại Giải vô địch bóng đá thế giới 2022 – Khu vực châu Á Vòng loại giải vô địch bóng đá châu Âu 2016 Vòng loại Giải vô địch bóng đá thế giớiTài liệu tham khảo
WikiPedia: Vòng_đời_đối_tượng