* 构造方法(构造器)
* 1.通过new关键字调用
* 2.构造方法虽然有返回值,但不能定义返回类型,不能用return返回某值,可以只用return;
* 3.我们如果没有定义构造器,系统会自己定义一个无参的构造函数,如果自己定义了那么系统不会自动添加
* 4.构造方法名必须与类名一致
*
* this的本质、用法 (this不能用于static方法中,this指的是对象,静态里没有对象)
* 本质:创建好的对象的地址,表示当前的对象
* 用法:1.下面1
* 2.下面2
*
* static(静态):1.静态修饰的是都从属于类
* 2.静态方法可以调用静态变量和方法,不能调用非静态的,非静态的方法可以调用静态的.
* 静态代码块是最先被加载执行的
*/
class Point{
double x,y;
static int q;
public Point(double x,double y) {
this(x);//2.调用Point (double x)构造方法,不能写Point (x),必须放第一句
this.y = y;
}
Point (double x){
this.x = x;//1.this的本质是 创建好的对象的地址,表示当前的对象
}
public double getDistance(Point p){
return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
}
static void ceshi(int a){
System.out.println("静态方法");
}
void diaoyon(){
ceshi(q);//非静态的可以调用静态的
}
}
public class Constructor_This_Static {
public static void main(String[] args) {
Point p = new Point(3.0, 4.0);
Point p2 = new Point(0.0, 0.0);
System.out.println(p.getDistance(p2));
}
}