Java面向对象 练习(类、对象、方法)
知识点:构造方法、继承、方法重载、方法重写
一、
定义一个点(Point)类,用来表示三维空间中的点(有三个坐标),要求如下:
1、可以生成具有特定坐标的点对象(构造方法);
2、提供可以设置三个坐标的方法(成员方法);
3、提供可以计算改点到另一个点的距离的方法(成员方法);
代码如下:
注:文件名为TestPoint.java(主类)
public class Point{ double x,y,z; //构造方法 public Point(double _x,double _y,double _z){ x = _x; y = _y; z = _z; } //可以 “设置三个坐标” 的方法 void setX(double _x){ x = _x; } void setY(double _y){ y = _y; } void setZ(double _z){ z = _z; }
//计算 “该点距其他点(p1)平均距离的平方” 的方法 public double getDistance(point p){ return (x - p.x)*(x - p.x) + (y - p.y)*(y - p.y) + (z - p.z)*(z - p.z); } public class TestPoint{ public static void main(String[] args){ Point p = new Point(1.0, 2.0, 3.0); //新建对象 Point p1 = new Point(0.0, 0.0, 0.0);//新建原点对象 system.out.println(p.getDistance(p1));//调用对象p的方法(p点距p1点的距离) }
//改变x点的坐标后重新计算p点距其他点的坐标 p.setX(5.0); system.out.println(p.getDistance(new Point(1.0, 1.0, 1.0))); } }
二、TestCircle程序分析
1、分析: Circle c1 = new Circle(new Point(1.0,2.0), 2.0);
Circle c2 = new Circle(5.0);
//对象: x、y、圆心、半径、面积 //方法: getX、getY、setX、setY // 坐标x点、y点 class Point { private double x; private double y; Point(double x1, double y1) { x = x1; y = y1; } 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; private double radius; Circle(Point p, double r) { //圆心、半径 o = p; radius = r; } Circle(double r) { o = new Point(0.0, 0.0); //圆心 为0 radius = r; //半径 } // 写一个方法,计算一个点是否在圆内 boolean contains(Point p) { double x = p.getX() - o.getX(); double y = p.getY() - o.getY(); if(x*x + y*y > radius * radius) 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 getRadius() { return radius;} public void setRadius(double r) { radius = r;} //圆面积 public double area() { return 3.14 * radius * radius; } } public class TestCircle { public static void main(String args[]) { Circle c1 = new Circle(new Point(1.0,2.0), 2.0); //圆心、半径 o = new Point(1.0,2.0) x = 1.0 y =2.0 radius = r = 2.0 Circle c2 = new Circle(5.0); // 圆心 o = new Point(0.0, 0.0) x = 0.0 y =0.0 半径 radius = r = 5.0 System.out.println("c1:("+c1.getO().getX()+"," +c1.getO().getY()+"),"+c1.getRadius()); System.out.println("c2:("+c2.getO().getX() +","+c2.getO().getY()+"),"+c2.getRadius()); System.out.println("c1 area = "+c1.area()); //3.14 * 2.0 * 2.0 System.out.println("c1 area = "+c2.area()); //3.14 * 5.0 * 5.0 c1.setO(5,6); // o.setX(5); o.setY(6); 即:x = 5; y = 6; c2.setRadius(9.0); //radius = r = 9.0; System.out.println("c1:("+c1.getO().getX()+"," +c1.getO().getY()+"),"+c1.getRadius()); System.out.println("c2:("+c2.getO().getX()+"," +c2.getO().getY()+"),"+c2.getRadius()); System.out.println("c1 area = "+c1.area()); //3.14 * 2.0 * 2.0 System.out.println("c1 area = "+c2.area()); //3.14 * 9.0 * 9.0 Point p1 = new Point(5.2, 6.3); // x = 5.2; y = 6.3; System.out.println(c1.contains(p1)); //判断 p1是否在圆c1里面(对象.方法) System.out.println(c1.contains(new Point(10.0,9.0))); } }