java构造方法

1原始代码

class Point {
    double x, y;
    public Point(double _x, double _y) {
        x = _x;
        y = _y;  
    }
    public double getDistance(Point p) {
        return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
    }
}
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);
        System.out.println(p.getDistance(origin));
    }
}

2逐步分析


class Point {
    double x, y;
    public Point(double _x, double _y) {
        x = _x;
        y = _y;  
        System.out.println(x);
        System.out.println(y);
        System.out.println();

        
    }
    public double getDistance(Point p) {
        //它是穿了一个对象进来
    	//Point p, 是 origin的对象实例的地址
    	//上面那个Point p 实例里面是 x=0.1, y=0.2
    	//所以,那个p.x,表示0.1
        System.out.println(p.x);
        System.out.println(p.y);

        /*        System.out.println();
        System.out.println();
        System.out.println(p.x);
        System.out.println();
        System.out.println(y);
        System.out.println();
        System.out.println(p.y);
        System.out.println();
*/      
        return Math.sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y));
    //    return "";
    }
}
public class TestConstructor {
    public static void main(String[] args) {
        Point p = new Point(3.0, 4.0);
        Point origin = new Point(0.1, 0.2);
        
//        System.out.println(p);
        
//        System.out.println();
        
//        System.out.println(origin);

//        System.out.println();

        System.out.println(p.getDistance(origin));
    }
}

3逐条注解

class Point{//定义一个Point类
    double x,y,z;//这个类有三个属性,x,y,z
    public Point(double _x,double _y,double _z){//构造函数给x,y,z赋初值
        x=_x;
        y=_y;
        z=_z;
    }
    public double getDistance(Point p){//一个public类型的成员函数计算Point p与开始赋值的点的(欧式)距离
        return Math.sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y)+(z-p.z)*(z-p.z));
    }
}
 
class Test{
    public static void main(String[] args){
        Point p=new Point(3.0,4.0,5.0);//建了一个坐标为(3.0,4.0,5.0)的点,记为p
        Point origin=new Point(0.0,0.0,0.0);//建了一个坐标为(0.0,0.0,0.0)的点,记为origin
        System.out.println(p.getDistance(origin));//计算p与origin两点间的距离并打印到控制台
         
    }
}

//-----------------------

 

 Point.java

public class Point {
	private double x;
	private double y;
	private double z;
	public double getX() {
		return x;
	}
	public void setX(double x) {
		this.x = x;
	}
	public double getY() {
		return y;
	}
	public void setY(double y) {
		this.y = y;
	}
	public double getZ() {
		return z;
	}
	public void setZ(double z) {
		this.z = z;
	}
	public Point(double x, double y, double z) {
		super();
		this.x = x;
		this.y = y;
		this.z = z;
	}
	/**
	 * 计算两个点的距离
	 * @param p
	 * @return
	 */
	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 p1 = new Point(1, 1, 1);
		Point p2 = new Point(2, 2, 2);
		System.out.println(p1.distance(p2));
	}
}

### Java 构造方法的用法及其示例 #### 什么是构造方法? 在 Java 中,构造方法是一种特殊的方法,用于创建并初始化对象的状态。它具有与类相同的名称,并且不返回任何数据类型(甚至 `void`)。当通过 `new` 关键字实例化一个新对象时,相应的构造方法会被调用来完成必要的初始化工作。 #### 默认构造方法 如果开发者未显式定义任何构造方法,则编译器会自动提供一个无参的默认构造方法[^1]。然而,一旦定义了一个自定义构造方法,默认构造方法将不再由编译器生成。 ```java public class MyClass { int value; // 自动提供的默认构造方法(如果没有定义其他构造函数) public MyClass() { this.value = 0; } } ``` #### 参数化的构造方法 参数化的构造方法允许传递初始值给对象属性,在创建对象的同时设置其状态。 ```java public class Person { String name; int age; // 带有参数的构造方法 public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{name='" + name + "', age=" + age + "}"; } } // 使用带参数的构造方法来创建对象 Person person = new Person("Alice", 30); System.out.println(person.toString()); // 输出: Person{name='Alice', age=30} ``` #### 静态工厂方法 vs 构造方法 尽管可以通过静态工厂方法替代传统的构造方法实现对象创建,但它们之间存在一些差异。例如,静态工厂方法可以拥有有意义的名字、返回缓存的对象或者子类型的实例等特性[^3]。 ```java class MyBeanFactory { private static final Map<String, MyBean> cache = new HashMap<>(); public static synchronized MyBean getMyBeanInstance(String key) { if (!cache.containsKey(key)) { cache.put(key, new MyBean()); } return cache.get(key); } } // 调用静态工厂方法获取对象实例 MyBean myBean = MyBeanFactory.getMyBeanInstance("uniqueKey"); ``` #### 复合模式中的构造方法应用 某些设计模式如单例模式也依赖于私有的构造方法防止外部随意实例化该类[^1]。 ```java public class SingletonExample { private static SingletonExample instance; // 私有化构造方法阻止外界直接访问 private SingletonExample() {} public static SingletonExample getInstance() { if(instance == null){ instance = new SingletonExample(); } return instance; } } ``` #### 注意事项 - 如果尝试除以零的操作将会抛出 ArithmeticException 异常;但是有一种特殊情况即最小整数值被负一相除不会引发异常而是保持原样[^4]。 - 功能测试关注的是应用程序的功能需求是否满足预期行为而不涉及内部结构细节[^5]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值