理解构造方法—例子

public class Person {
    int id;
    int age = 20;
    
    Person(int _id, int _age) {
        id = _id;
        age = _age;
    }                         
    
     //构造方法如上
    
    
    
    
    
    public static void main(String[] args) {
        Person tom = new Person(1,25);//调用构造方法 _id和_age在栈内,将1和25传入方法内,局部变量就会消失。
        
        Point p = new Point();  //当调用下面类的方法中不写构造方法时,系统会默认空的调用
        
    }
}




class Point {
    
    Point() {} // 系统默认的构造方法 且x,y初始为0
    int x;
    int y;
     
}
原文地址:https://www.cnblogs.com/lsswudi/p/11221737.html