重载构造函数(相同运算结果,不同性能比较)

 1 import java.awt.Point;
 2   class Box2  
 3     { int x1;
 4       int y1;
 5       int x2;
 6       int y2;
 7      
 8   Box2(int x,int y,int z,int w)
 9      { this.x1 = x;
10        this.y1 = y;
11        this.x2 = z;
12        this.y2 = w;
13       }
14   Box2(Point topleft,Point bottomright) 
15       { this(topleft.x,topleft.y,bottomright.x,bottomright.y); 
16        }
17   Box2(Point topleft,int wide,int height)
18       { this(topleft.x,topleft.y,(topleft.x += wide),(topleft.y -= height)); 
19        } 
20  public static void main(String[] args)
21        { Box2 box0 = new Box2(1,2,3,4);
22          System.out.print(box0.x1+" "+box0.y1+" "+box0.x2+" "+box0.y2);
23          Box2 box1 = new Box2(new Point(1,2),3,4);
24          System.out.print(box1.x1+" "+box1.y1+" "+box1.x2+" "+box1.y2);
25          Box2 box2 = new Box2(new Point(1,2),new Point(3,4));
26          System.out.print(box2.x1+" "+box2.y1+" "+box2.x2+" "+box2.y2);
27         }
28       }  

 

 

 1 import java.awt.Point;
 2 class Box3 
 3   { int x1;
 4     int y1;
 5     int x2;
 6     int y2;
 7     
 8     Box3(int x,int y,int z,int w)
 9         { this.x1 = x;
10           this.y1 = y;
11           this.x2 = z;
12           this.y2 = w;
13          }
14     Box3(Point lefttop,Point bottomright)
15          { this.x1= lefttop.x;
16            this.y1= lefttop.y;
17            this.x2= bottomright.x;
18            this.y2= bottomright.y;
19           }
20      Box3(Point lefttop,int width,int height)
21           {this.x1 = lefttop.x;
22            this.y1 = lefttop.y;
23            this.x2 = lefttop.x + width;
24            this.y2 = lefttop.y - height;
25            }
26       void printBox()
27            { System.out.print(x1+" "+y1+" "+x2+" "+y2+" ");
28             }
29        public static void main (String[] args)
30        { Box3 box3;
31          box3 = new Box3(1,2,3,4);
32          box3.printBox();
33          box3 = new Box3(new Point(1,2),3,4);
34          box3.printBox();
35          box3 = new Box3(new Point(1,2),new Point(3,4));
36          box3.printBox();
37         }
38      }

 

 

 1 import java.awt.Point;
 2 class Box4 
 3   { int x1;
 4     int y1;
 5     int x2;
 6     int y2;
 7     
 8     Box4(int x,int y,int z,int w)
 9         { this.x1 = x;
10           this.y1 = y;
11           this.x2 = z;
12           this.y2 = w;
13          }
14     Box4(Point lefttop,Point bottomright)
15          { this(lefttop.x,lefttop.y,bottomright.x,bottomright.y);
16           }
17      Box4(Point lefttop,int width,int height)
18           {this(lefttop.x,lefttop.y,lefttop.x += width,lefttop.y -= height);
19            }
20       void printBox()
21            { System.out.print(x1+" "+y1+" "+x2+" "+y2+" ");
22             }
23        public static void main (String[] args)
24        { Box4 box4;
25          box4 = new Box4(1,2,3,4);
26          box4.printBox();
27          box4 = new Box4(new Point(1,2),3,4);
28          box4.printBox();
29          box4 = new Box4(new Point(1,2),new Point(3,4));
30          box4.printBox();
31         }
32      }

 

posted @ 2015-12-20 23:10  lubocsu  阅读(268)  评论(0编辑  收藏  举报