กก
class TwoDPoint {
double x;
double y;
void print() {
System.out.println("(" + this.x + "," + this.y + ")");
}
void print(int n) {
for (int i = 0; i < n; i++) {
System.out.println("(" + this.x + "," + this.y + ")");
}
}
}
}
To use this class, you might have some lines like
these in a separate class and file:
กก
TwoDPoint origin = new TwoDPoint();
origin.x = 0.0;
origin.y = 0.0;
origin.print(10);
Note that there are two different print()
methods. One takes an argument. One doesn't. As long as the argument lists
can disambiguate the choice, this is allowed. This is called
overloading.
Also note, that the System.out.println()
we've been using all along is an overloaded method.
main(String[] args) is a
non-overloaded method that has an array of strings as arguments.
|