package OOB;
public class This_construct {
public static void main(String[] args){
Student1 st=new Student1();
// System.out.println(st.Student1);
// st.Student1();
}
}
class Student1{
public Student1(){
this("hello");
}
public Student1(String name){
System.out.println("print "+name);
}
}
关于构造方法:
-
构造方法的特征
• 它具有与类相同的名称; • 它不含返回值; • 它不能在方法中用 return 语句返回一个值; • 当自定义了构造方法后,编译器将不再自动创建不带参数的构造方法 。 • 在构造方法里不含返回值的概念是不同于 “void” 的,在定义构造方法时加了 “void” ,结果这个方法就不再被自动调用了。
-
构造方法的作用:
当一个类的实例对象刚产生时,这个类的构造方法就会被自动调用,我们可以在这个方法中加入要完成初始化工作的代码。



