class A{
int x;
public void A(){
System.out.println("这是父类的构造函数");
new jichen().inia();
}
}
public class jichen extends A {
/*public void A(){
System.out.println("重写的父类构造");
}
*/
public void jichen(){
super.A();
System.out.println("这是bb类构造函数");
}
public void inia(){
System.out.println("子类初始化"+x);
x=555555;
}
public static void main(String args[]){
jichen a=new jichen();
System.out.println(new A().x);
a.x=66666;
System.out.println(a.x);
}
}
/*执行结果,只有两个数值
0
66666
*/
int x;
public void A(){
System.out.println("这是父类的构造函数");
new jichen().inia();
}
}
public class jichen extends A {
/*public void A(){
System.out.println("重写的父类构造");
}
*/
public void jichen(){
super.A();
System.out.println("这是bb类构造函数");
}
public void inia(){
System.out.println("子类初始化"+x);
x=555555;
}
public static void main(String args[]){
jichen a=new jichen();
System.out.println(new A().x);
a.x=66666;
System.out.println(a.x);
}
}
/*执行结果,只有两个数值
0
66666
*/
public A()才是构造函数,没有返回直类型
(1)构造方法的方法名必须与类名相同。
(2)构造方法没有返回值类型,不能使用return语句。---(不能有返回值,你定义的有void返回值,虽然返回的为空)
(3)构造方法的主要作用是完成对象的初始化工作,它能够把定义对象时的参数传给对象的域。
(4)一个类可以定义多个构造方法,如果在定义类时没有定义构造方法,则编译系统会自动插入一个无参数的默认构造方法,这个构造方法不执行任何代码。
(5)构造方法可以重载,以参数的个数,类型,顺序。
different from a void return value, in which the method returns nothing but you still have
the option to make it return something else. Constructors return nothing and you don’t have
an option (the new expression does return a reference to the newly created object, but the
constructor itself has no return value). If there were a return value, and if you could select
your own, the compiler would somehow need to know what to do with that return value.
Thinking in java 的原文,大意就是构造器是一个特殊方法,没有返回值,于void 这种也不同