构造方法
调用构造方法的演示
Student类public class Student {
public Student() {
System.out.println("我是Student的无参构造函数");
}
public Student(int a) { //方法重载
System.out.println(a);
}
public Student(String name,int age) { //方法重载
System.out.println(name+"\t"+age);
}
}
Text1类
public class Text1 {
public static void main(String[] args) {
//匿名调用也是可以的,如new Student();
//只要创建对象,就会自动调用相应的构造方法
Student s = new Student(3); //调用 public Student(int a)
Student t = new Student("jack",44); //调用public Student(String name,int age)
Student x = new Student(); //调用 public Student()
}
}
结果
3
jack 44
我是Student的无参构造函数