构造方法
package com.itcode.demo;
/**
* @author: 成都码到功成学员
* @description:
* 构造方法
* 求两个坐标点的距离
*/
class Point{
double a;
double b;
public Point(double _a, double _b) {
a=_a;
b=_b;
}
public double getDistance(Point p) {
return Math.sqrt((a-p.a)*(a-p.a) + (b-p.b)*(b-p.b));
}
}
public class TestConstructor {
public static void main(String[] args) {
// TODO Auto-generated method stub
// 定义两个坐标
Point p1 = new Point(8.0, 6.0);
Point p2 = new Point(0.0, 0.0);
System.out.println(p1.getDistance(p2));
}
}构造方法重载
Last updated