- Each method has a signature: its name together with the number and types of its parameters
Methods Signatures String toString()
void move(int dx, int dy)
void paint(Graphics g)()
(int, int)
(Graphics) - Two methods can have the same name if they have different signatures. They are overloaded.
Overloading Example
Public class Point {
protected double x, y;
public Point() {
x=0.0; y=0.0;
}
public Point (double x, double y) {
this.x=x; this.y=y;
}
/**calculate the distance between this point and the other point*/
public double distance (Point other) {
double dx=this.x-other.x;
double dy=this.y-other.y;
return Math.sqrt(dx*dx+dy*dy);
}
/**calculate the distance between this point and (x,y)*/
public double distance(double x, double y) {
double dx = this.x - x;
double dy = this.y - y;
return Math.sqrt(dx * dx + dy * dy);
}
/**calculate the distance between this point and the origin*/
public double distance() {
return Math.sqrt(x * x + y * y);
}
// other methods
}
0 comments:
Post a Comment