构造方法和重载

实现点(Point)类
横坐标x和纵坐标y
实现圆(Circle)类和矩形(Rectangle)类   
  1) 圆(Circle)类包含圆心(Point p)和半径(int r)两个属性   
  2) 矩形(Rectangle)类包含中心点(Point p)、宽(int w)、高(int h)三个属性   
  3) 提供合理的构造器   
  4) 提供方法:   
. 计算当前图形的面积getArea();    pi*r*r     w*h
. 计算当前图形是否包含(contains(Point p))指定的点;   

. 重载方法  contains(int x, int y) 图形是否包含坐标x,y;

Point类

public class Point {
	public int x;
	public int y;

	public  Point(int a,int b) {
		this.x = a;
		this.y = b;
		System.out.println("点的坐标为:" +"(" +x + "," + y + ")");
	}
	
	public static void main(String[] args) {
		Point point = new Point(5,2);
		
	}
}


Circle类:

public class Circle {
	Point p;
	int r;
/*	
	public Circle() {
		this((new Point(5,6)),5);
	}
*/
  public Circle(int r) {
	  this((new Point(0,0)),r);
	}


	
	public Circle(Point p,int r) {
		this.p = p;
		this.r= r;
	}
	


    public Circle(int x,int y,int r) {
    	this((new Point(x,y)),r);
	}


	public Circle getArea() {
		double s = r *r *3.14;
		System.out.println("圆的面积为:" + s);
		return this;
	}
	public Circle getZhou() {
		double z = r *2 *3.14;
		System.out.println("圆的周长为:" + z);
		return this;
	}
	
	public static void main(String[] args) {
//		Circle c = new Circle();
//		System.out.println("圆的半径为:" + c.r);
//		c.getArea().getZhou();
	}
	
}

Rectangle类:

public class Rectangle {
	Point p;
	public int w;
	public int h;
	
	public Rectangle(int w,int h) {
		this(new Point(0, 0),w,h);
	}
	
	
	public Rectangle(Point p,int w,int h){
		this.p = p;
		this.w = w;
		this.h = h;
	}
	public Rectangle(int a,int b,int w,int h){
		this(new Point(a, b),w,h);
	}
	
	
	public void getArea() {
		int s = w*h;
		System.out.println("矩形的面积为:" + s);
	}
	public void contains(double x,double y){
		double w2 = this.w / 2;
		double h2 = this.h/2;
		double t = Math.sqrt((this.w-x)*(this.w-x)+(this.h-y)*(this.h-y));
		if(t <= w2 && t<= h2) {
			System.out.println("点(" + x + ", " + y + ")包含在图形内!");
		}else {
			System.out.println("点(" + x + ", " + y + ")不包含在图形内!");			
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
//		Rectangle re = new Rectangle(new Point(2, 2),5, 6);
//		re.getArea();
//		re.contains(10, 5);
	}

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值