构造器
构造器
类:
public class persontext {
//一个类即使什么都不写,它也会存在一个方法
//显示的定义构造器
String name;
//1.使用new关键字,本质是在调用构造器
//2.用来初始化值
public persontext() {
}
public persontext(String name) {
this.name = name;
}
//使用快捷键alt+ins
//有参构造:一旦定义了有参构造,无参就必须显示定义
实例化:
public class person {
public static void main(String[] args) {
//new 实例化了一个对象
persontext person = new persontext("小杜");
System.out.println(person.name);
}
}
浙公网安备 33010602011771号