分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,并实现一下功能

/**
 * @author: 袁
 * @date: 2022-09-22 15:35
 * @desc:分别编写两个类Point2D,Point3D来表示二维空间和三维空间的点,使之满足下列要求:
 */
class Point2D{
    int x;
    int y;

    public  Point2D(){
        x = 0;
        y = 0;
    }

    public  Point2D(int x,int y)
    {
        this.x = x;
        this.y = y;
    }

    public void offset(int a, int b){
        x += a;
        y += b;
    }
}

class Point3D extends Point2D{
    int x;
    int y ;
    int z;

    public Point3D(int x,int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public Point3D(Point2D p, int z){
        this.x = p.x;
        this.y = p.y;
        this.z = z;
    }

    public void offset(int a, int b,int c){
        x += a;
        y += b;
        z += c;
    }

    public static void main(String[] args) {
        Point2D p2d1 = new Point2D(1,1);
        Point2D p2d2 = new Point2D(3,3);
        double p2dis = Math.sqrt(Math.pow(p2d2.x - p2d1.x,2) + Math.pow(p2d2.y - p2d1.y,2));
        System.out.println(p2dis);

        Point3D p3d1 = new Point3D(1,1,1);
        Point3D p3d2 = new Point3D(3,3,3);
        double p3dis = Math.sqrt(Math.pow(p3d2.x - p3d1.x,2) + Math.pow(p3d2.y - p3d1.y,2)+Math.pow(p3d2.z - p3d1.z,2));
        System.out.println(p3dis);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值