Chinaunix首页 | 论坛 | 博客
  • 博客访问: 7723895
  • 博文数量: 961
  • 博客积分: 15795
  • 博客等级: 上将
  • 技术积分: 16612
  • 用 户 组: 普通用户
  • 注册时间: 2010-08-07 14:23
文章分类

全部博文(961)

文章存档

2016年(1)

2015年(61)

2014年(41)

2013年(51)

2012年(235)

2011年(391)

2010年(181)

分类: Java

2012-04-09 22:31:46


点击(此处)折叠或打开

  1. class Point {
  2.     private double x;
  3.     private double y;
  4.     
  5.     Point(double x1, double y1) {
  6.         x = x1;
  7.         y = y1;
  8.     }
  9.     
  10.     public double getX() { return x; }
  11.     public double getY() { return y; }
  12.     public void setX(double i) { x = i; }
  13.     public void setY(double i) { y = i; }
  14. }

  15. class Circle {
  16.     private Point o;
  17.     private double radius;
  18.     
  19.     Circle(Point p, double r) {
  20.         o = p;
  21.         radius = r;
  22.     }
  23.     
  24.     Circle(double r) {
  25.         o = new Point(0.0, 0.0);
  26.         radius = r;
  27.     }
  28.     
  29.     boolean contains(Point p) {
  30.         double x = p.getX() - o.getX();
  31.         double y = p.getY() - o.getY();
  32.         
  33.         if(x*x + y*y > radius * radius) return false;
  34.         else return true;
  35.     }
  36.     
  37.     
  38.     public void setO(double x, double y) {
  39.         o.setX(x);
  40.         o.setY(y);
  41.     }
  42.     
  43.     public Point getO() { return o; }
  44.     public double getRadius() { return radius;}
  45.     public void setRadius(double r) { radius = r;}
  46.     
  47.     public double area() {
  48.         return 3.14 * radius * radius;
  49.     }
  50. }

  51. public class TestCircle {
  52.     public static void main(String args[]) {
  53.         Circle c1 = new Circle(new Point(1.0,2.0), 2.0);
  54.         Circle c2 = new Circle(5.0);
  55.         System.out.println("c1:("+c1.getO().getX()+","
  56.             +c1.getO().getY()+"),"+c1.getRadius());
  57.         System.out.println("c2:("+c2.getO().getX()
  58.             +","+c2.getO().getY()+"),"+c2.getRadius());
  59.         System.out.println("c1 area = "+c1.area());
  60.         System.out.println("c1 area = "+c2.area());
  61.         c1.setO(5,6);
  62.         c2.setRadius(9.0);
  63.         System.out.println("c1:("+c1.getO().getX()+","
  64.             +c1.getO().getY()+"),"+c1.getRadius());
  65.         System.out.println("c2:("+c2.getO().getX()+","
  66.             +c2.getO().getY()+"),"+c2.getRadius());
  67.         System.out.println("c1 area = "+c1.area());
  68.         System.out.println("c1 area = "+c2.area());
  69.         
  70.         Point p1 = new Point(5.2, 6.3);
  71.         System.out.println(c1.contains(p1));
  72.         System.out.println(c1.contains(new Point(10.0,9.0)));
  73.     }
  74. }
课件: 构造方法重载.pdf   
阅读(1420) | 评论(0) | 转发(2) |
0

上一篇:Java 方法重载

下一篇:NW-III软件

给主人留下些什么吧!~~