构造方法

public class Test {
    
    //构造方法,每一个类中都有构造方法,当代码中没有显式的构造方法时,
    //程序在执行过程中,会创建一个公开的无参构造方法
    //当代码中有了显式的构造方法,程序就不会自动创建公开无参的构造方法
    //构造方法的作用:一般是为成员变量赋初始化值
    
    public int a;//成员变量
    
    public Test(int i) {
        a = i;
    }

    public static void main(String[] args) {
        Test t = new Test(1);
        System.out.println(t.a);
    }

}
原文地址:https://www.cnblogs.com/xiaotao520/p/9130383.html