X++学习(三)

实例化,构造函数,析构函数


1.实例化

Point myPoint;

myPoint = new Point();

对象的实例成员,必须通过访问器访问,不能直接通过变量名直接访问.

myPoint.x = 10.0; //错误的.

2.构造函数

class Point

{

int x; // instance variables

int y; // defined in classDeclaration

}

void new(real a=10.0, real b=10.0)

{ //Constructor to initialize to default value

x = a;

y = b;

}

用new做为构造函数,该函数可以有参数,但不能有返回类型.这个把C++之父天才的主意给打破了,哈哈.

3.析构函数

// From any method in a class

{

. . .

if(Condition)

this.Finalize();

. . .

}

可以在Finalize()中进行对象的清理工作,该方法不会被系统隐式调用,必须在程序中显式调用.当调用Finalize()的时候,MorphX会把这个对象从内存中清理掉.Finlize()还可以放一些其他的清理的代码,比如如果类调用了Dll模块,可以在这里将其释放.

感觉X++的析构函数跟C++的析构函数是一回事,那么X++里有没有类似C#里的Dispose那?貌似没有.自己写吧.

4.subClasses

class Point

{

real x; /* instance variable */

real y; /* instance variable */

New(real _x, real _y)

{ // constructor to initialize x and y

x = _x;

y = _y;

}

}

class ThreePoint extends Point

{

real z; //The z coordinate of the point

New(real _x, real _y, real _z)

{

super(_x, _y); // initialize the coordinates

z = _z;

}

}

原文地址:https://www.cnblogs.com/Farseer1215/p/330835.html