构造器

构造器
类:
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);
}
}

posted @ 2022-05-12 21:33  韶光贱  阅读(19)  评论(0)    收藏  举报