63.从类中实例化一个对象

BACKWARDFORWARD



假设我们已经为一个可移动的点定义了一个类, 称为 mPoint . 这个点有位置(position)[x,y]和颜色color, 可以以步长(dx,dy) 移动

mPoint BunchofPoints[2]; /* defines BunchofPoints to be 2 dimensional array of mPoint objects -- it does not instantiate BunchofPoints */

FirstPoint = new mPoint(); /* Allocates an instance of mPoint and invokes Constructor of mPoint to Initialize */

FirstPoint.dx=5;

FirstPoint.dy=5; /* Redefines values of instance variables dx,dy for object FirstPoint */

mPoint could be defined by

import java.awt.*;

public class mPoint {

int x, y;

int dx = 1, dy = 1;

Color color = Color.black;

}


Copyright: NPACT BACKWARDFORWARD