Java构造方法

xiaoxiao2021-02-28  90

package com.text.come; public class Point{     private int x,y,z;     public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } public int getZ() { return z; } public void setZ(int z) { this.z = z; } //通过构造方法初始化对象的属性     public Point(int x,int y,int z){     this.x=x;     this.y=y;     this.z=z;     }      //求两点之间的距离     public double distance(Point p){     return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z));     }     public static void main(String[] args) {     Point p =new Point(3,4,5); System.out.println(p.x); Point q = new Point(6, 8, 10); System.out.println(p.distance(q)); } }
转载请注明原文地址: https://www.6miu.com/read-84421.html

最新回复(0)