您的位置:首页 > 其它

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

2015-12-20 23:10 323 查看
import java.awt.Point;
class Box2
{ int x1;
int y1;
int x2;
int y2;

Box2(int x,int y,int z,int w)
{ this.x1 = x;
this.y1 = y;
this.x2 = z;
this.y2 = w;
}
Box2(Point topleft,Point bottomright)
{ this(topleft.x,topleft.y,bottomright.x,bottomright.y);
}
Box2(Point topleft,int wide,int height)
{ this(topleft.x,topleft.y,(topleft.x += wide),(topleft.y -= height));
}
public static void main(String[] args)
{ Box2 box0 = new Box2(1,2,3,4);
System.out.print(box0.x1+" "+box0.y1+" "+box0.x2+" "+box0.y2);
Box2 box1 = new Box2(new Point(1,2),3,4);
System.out.print(box1.x1+" "+box1.y1+" "+box1.x2+" "+box1.y2);
Box2 box2 = new Box2(new Point(1,2),new Point(3,4));
System.out.print(box2.x1+" "+box2.y1+" "+box2.x2+" "+box2.y2);
}
}

 

 

import java.awt.Point;
class Box3
{ int x1;
int y1;
int x2;
int y2;

Box3(int x,int y,int z,int w)
{ this.x1 = x;
this.y1 = y;
this.x2 = z;
this.y2 = w;
}
Box3(Point lefttop,Point bottomright)
{ this.x1= lefttop.x;
this.y1= lefttop.y;
this.x2= bottomright.x;
this.y2= bottomright.y;
}
Box3(Point lefttop,int width,int height)
{this.x1 = lefttop.x;
this.y1 = lefttop.y;
this.x2 = lefttop.x + width;
this.y2 = lefttop.y - height;
}
void printBox()
{ System.out.print(x1+" "+y1+" "+x2+" "+y2+" ");
}
public static void main (String[] args)
{ Box3 box3;
box3 = new Box3(1,2,3,4);
box3.printBox();
box3 = new Box3(new Point(1,2),3,4);
box3.printBox();
box3 = new Box3(new Point(1,2),new Point(3,4));
box3.printBox();
}
}

 

 

import java.awt.Point;
class Box4
{ int x1;
int y1;
int x2;
int y2;

Box4(int x,int y,int z,int w)
{ this.x1 = x;
this.y1 = y;
this.x2 = z;
this.y2 = w;
}
Box4(Point lefttop,Point bottomright)
{ this(lefttop.x,lefttop.y,bottomright.x,bottomright.y);
}
Box4(Point lefttop,int width,int height)
{this(lefttop.x,lefttop.y,lefttop.x += width,lefttop.y -= height);
}
void printBox()
{ System.out.print(x1+" "+y1+" "+x2+" "+y2+" ");
}
public static void main (String[] args)
{ Box4 box4;
box4 = new Box4(1,2,3,4);
box4.printBox();
box4 = new Box4(new Point(1,2),3,4);
box4.printBox();
box4 = new Box4(new Point(1,2),new Point(3,4));
box4.printBox();
}
}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐