//学生类
public class Student {
String name;
int age;
//不是用构造器时,会有一个默认的无参构造器
//alt+insert:导入构造器
//1.使用new关键字,本质是在调用构造器
//2.用来初始化值
//无参构造
public Student() {
}
//有参构造:一旦定义了有参构造,无参就必须显示定义
public Student(String name, int age) {
this.name = name;
this.age = age;
}
/*
构造器:
1.和类名相同
2.没有返回值
作用:
1.new 本质是在调用构造方法
2.初始化对象的值
注意点:
1.定义有参构造之后,如果想使用无参构造,显示的定义一个无参构造
alt+insert:导入构造器
当前这个类.属性=传入的值
this. =
*/
}
public class Application {
public static void main(String[] args) {
Student student = new Student("du",20);
System.out.println(student.name);//du
System.out.println(student.age);//20
}
}