Chinaunix首页 | 论坛 | 博客
  • 博客访问: 631704
  • 博文数量: 1008
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 5175
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-31 09:44
文章分类
文章存档

2012年(1008)

我的朋友

分类:

2012-08-01 11:01:42

原文地址:Java 构造方法重载 作者:luozhiyong131


点击(此处)折叠或打开

  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   
阅读(130) | 评论(0) | 转发(0) |
0

上一篇:NW-III软件

下一篇:Java 方法重载

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