版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42528266/article/details/102948464
package com.cwl.base.day03;
/**
* @program: java_base
* @description: 测试构造器
* @author: ChenWenLong
* @create: 2019-11-07 09:32
**/
public class TestConstructor {
public static void main(String[] args) {
Point p = new Point(3.0, 4.0);
Point origin = new Point(0.0, 0.0);
Point p2 = new Point();
System.out.println(p.getDistance(origin));
}
}
class Point {
double x, y;
//构造方法名称和类名必须保持一致
public Point(double _x, double _y) {
x = _x;
y = _y;
}
Point(){
}
public double getDistance(Point p) {
return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
}
}
代码示例
package com.cwl.base.day03;
/**
* @program: java_base
* @description: 测试This关键字
* @author: ChenWenLong
* @create: 2019-11-07 09:33
**/
public class TestThis {
int a, b, c;
TestThis(int a, int b) {
this.a = a;
this.b = b;
}
TestThis(int a, int b, int c) {
this(a,b);
this.c = c;
}
void sing() {
}
void eat() {
this.sing(); // 调用本类中的sing();
System.out.println("你妈妈喊你回家吃饭!");
}
public static void main(String[] args) {
TestThis hi = new TestThis(2, 3);
hi.eat();
}
}
import java.util.Date;
import java.util.*; //导入该包下所有的类。会降低编译速度,但不会降低运行速度。