JAVA笔记140-使用this语句解决构造器重载相互调用问题

//创建点对象
class Point{
  private int x;
      private int y;

  Point(int x,int y){
    this.x = x;
    this.y = y;
  }
  public int getX(){
    return x;
  }
  public int getY(){
    return y;
  }
}

//创建圆对象
class Circle{
  private int r;//圆的半径

  Circle(int r){
    this.r = r;
  }

  int judge(Point p){
    int xxyy = p.getX() * p.getX() + p.getY() * p.getY();
    int rr = this.r * this.r;
    if(xxyy > rr){
      return 1;
    }else if(xxyy < rr){
      return 0;
    }else{
      return -1;
    }
}
}

 


//测试:判断点与圆之间的关系
public class thisDemo{
  public static void main(String[] args){
    Point p = new Point(3,4);//点的位置
    Circle c = new Circle(5);//创建圆
    int ret = c.judge(p);
    System.out.println(ret);
  }
}

posted @ 2017-05-10 18:01  FallenLunatic  阅读(244)  评论(0编辑  收藏  举报