用java分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点怎么做?

2024-11-01 09:01:25
推荐回答(1个)
回答1:

/*

* Project:         空间点

* Author:       searchpcc(xixi,11级,20112866)

* File:         TuXing.java

* Instruction: 主程序

* Time:         2012-11-07

*/

class Point2D{

int x;

int y;

public Point2D(){

}

public Point2D(int x, int y){

this.x = x;

this.y = y;

}

public void offset(int a, int b){

x = a;

y = b;

this.x = this.x + 1;

this.y = this.y + 1;

System.out.print(x);

System.out.print(y);

}

double distan(Point2D p1, Point2D p2){

return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)));

}

}

class Point3D extends Point2D{

int z;

public Point3D(int x, int y, int z){

super(x, y);

this.z = z;

}

public Point3D(){

}

public Point3D(Point2D p, int z){

super(p.x, p.y);

this.z = z;

}

public void offset(int a, int b,int c){

x = a;

y = b;

z = c;

this.x = this.x + 1;

this.y = this.y + 1;

this.z = this.z + 1;

}

double distan(Point3D p1, Point3D p2){

return(Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)+(p1.z-p2.z)*(p1.z-p2.z)));

}

}

public class KongJian1{

public static void main(String[] args){

Point2D p1 = new Point2D(3,3);

p1.offset(3, 3);

Point2D p2 = new Point2D(2,2);

Point3D p3 = new Point3D(4,4,4);

p3.offset(4,2,3);//可以进行移动

Point3D p4 = new Point3D(5,5,5);

System.out.println("p1和p2之间的距离为:");

System.out.println(new Point2D().distan(p1,p2));

System.out.println("p3和p4间的距离为:");

System.out.println(new Point3D().distan(p3,p4));

}

}

Java是一门面向对象编程语言,不仅吸收了C++语言的各种优点,还摒弃了C++里难以理解的多继承、指针等概念,因此Java语言具有功能强大和简单易用两个特征。Java语言作为静态面向对象编程语言的代表,极好地实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。

Java具有简单性、面向对象、分布式、健壮性、安全性、平台独立与可移植性、多线程、动态性等特点。Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。