Constructor-ууд нь class-тай ижил нэртэй method. Жишээ нь,
class TwoDPoint {
double x;
double y;
TwoDPoint(double xvalue, double yvalue) {
this.x = xvalue;
this.y = yvalue;
}
String getAsString() {
return "(" + this.x + "," + this.y + ")";
}
void setX(double value) {
this.x = value;
}
void setY(double value) {
this.y = value;
}
double getX() {
return this.x;
}
double getY() {
return this.y;
}
}
Constructor-ууд class-д объект үүсгэхдээ new түлхүүр үгийг ашигладаг.
TwoDPoint origin = new TwoDPoint(0.0, 0.0);
System.out.println("The x coordinate is " + origin.getX());
class TwoDPoint {
double x;
double y;
TwoDPoint(double xvalue, double yvalue) {
this.x = xvalue;
this.y = yvalue;
}
String getAsString() {
return "(" + this.x + "," + this.y + ")";
}
void setX(double value) {
this.x = value;
}
void setY(double value) {
this.y = value;
}
double getX() {
return this.x;
}
double getY() {
return this.y;
}
}
Constructor-ууд class-д объект үүсгэхдээ new түлхүүр үгийг ашигладаг.
TwoDPoint origin = new TwoDPoint(0.0, 0.0);
System.out.println("The x coordinate is " + origin.getX());
No comments:
Post a Comment