วันศุกร์ที่ 16 กรกฎาคม พ.ศ. 2553

Overloading Method

Definition Overloading: Overloading refers to the ability to allow different methods of constructors of a class to share the same name. The name is said to be overloaded with multiple implementations.

The legality of overloading depends on the signatures of the methods or constructors being overloaded. The signature of a methods or constructor consists of the name of  the method and a list of the types of its parameters.Note that the return type,parameter names and final designations of parameters are not part of the signature. Parameter order,however,is significant. The following are some examples of methods and their signatures.

Method Signature
String toString() toString()
void move(int dx,int dy) move(int,int)
void move(final int dx,final int dy) move(int,int)
void paint(Graphics g) paint(Graphics)

The Rule of Overloading: Two methods of constructors in the same class can be overloaded,i.e.,sharing the same name,if they have either different numbers of parameters or the same number of parameters but of different types.In other words,no two methods or constructors in the same class may have identical signatures.

The following class declaration illustrates overloaded constructors and overloaded methods:

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(x,y) */
    public double distance(int x,int y){
        double dx = this.x - (double) x;
        double dy = this.y - (double) 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 method
}

When an overloaded method is called, the number and the types of the arguments are used to determine the signature of the method that will be invoked.Overloading is resolved at compile time,as in the following code segment:

Point p1 = new Point();                               //invoke Point()
Point p2 = new Point(20.0,30.0);                 //invoke Point(double,double)
p2.distance(p1);                                         //invoke distance(Point)
p2.distance(50.0,60.0);                               //invoke distance(double,double)
p2.distance(50,60);                                    //invoke distance(int,int)
p2.distance();                                            //invoke distance()

ไม่มีความคิดเห็น:

แสดงความคิดเห็น