知識點:構造方法、繼承、方法重載、方法重寫
1、
定義一個點(Point)類,用來表示三維空間中的點(有三個座標),要求以下:java
一、能夠生成具備特定座標的點對象(構造方法);spa
二、提供能夠設置三個座標的方法(成員方法);code
三、提供能夠計算改點到另外一個點的距離的方法(成員方法);對象
代碼以下:blog
注:文件名爲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))); } }
2、TestCircle程序分析
一、分析: Circle c1 = new Circle(new Point(1.0,2.0), 2.0);get
Circle c2 = new Circle(5.0);class
//對象: 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))); } }