首页 > 其他 > 详细

面向对象3

时间:2014-11-14 22:38:38      阅读:351      评论:0      收藏:0      [点我收藏+]


class Point {
 private double x;//成员变量
 private double y;//成员变量

//构造方法
 Point (double _x,double _y){
  x = _x;
  y = _y;
  }
  //方法
 public double getX() {
  return x;
  }
  
 public double getY() {
  return y;
  }
  
 public void setX(double i) {
  x = i;
  }
  
 public void setY(double i) {
  y = i;
  }
 }
 
class Circle {
 private Point o;//Point类的复用
 private double redius;//成员变量

//够着方法
 Circle (Point p,double r){
  o = p;
  redius =r;
  }
  //重载
 Circle (double r){
  o = new Point(0.0, 0.0);
  redius = r;
  }
  
 boolean contains(Point p){
  double x = p.getX() - o.getX();
  double y = p.getY() - o.getY();
  if ((x*x + y*y) > redius*redius)
    return false;
  else
    return true;
  }
  
 public void setO(double x,double y){
  o.setX(x);
  o.setY(y);
  }
 
 public Point getO(){
  return o;
  }
  
 public double getRedius(){
  return redius;
  }
  
 public void setRedius(double r){
  redius = r;
  }
  
 public double area(){
  return 3.14 * redius * redius;
  }
 }
 
public class TestCircle{
 public static void main (String[] args){
  Circle c1 = new Circle(new Point(1.0, 2.0), 2.0);
  Circle c2 = new Circle(3.0);
  System.out.println("c1:("+c1.getO().getX() + "," + c1.getO().getY()
  + ") " + "redius = " + c1.getRedius());
  System.out.println("c2:("+c2.getO().getX() + "," + c2.getO().getY()
  + ") " + "redius = " +  c2.getRedius());
  System.out.println("c1:area= " + c1.area() );
  System.out.println("c2:area= " + c2.area());
  c1.setO(5.0,6.0);
  c2.setRedius(6.0);
  System.out.println("c1:("+c1.getO().getX() + "," + c1.getO().getY()
  + ") " + "redius = " +  c1.getRedius());
  System.out.println("c2:("+c2.getO().getX() + "," + c2.getO().getY()
  + ") " +  "redius = " + c2.getRedius());
  System.out.println("c1:area= " + c1.area() );
    System.out.println("c2:area= " + c2.area());
    Point p1 = new Point(5.2, 6.3);
    System.out.println(c1.contains(p1));
    System.out.println(c1.contains(new Point(10.0, 9.0)));
  }
 }
 

面向对象3

原文:http://www.cnblogs.com/dingxiaoblog/p/4098275.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!