求解答,我是新手,为什么这构造函数没有执行。

weixin_40446540 2017-09-29 08:45:12
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
*/
...全文
310 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
自由自在_Yu 2017-09-30
  • 打赏
  • 举报
回复
看一下构造函数是怎么定义的,你这不是构造函数 (1)构造方法的方法名必须与类名相同。 (2)构造方法没有返回值类型,不能使用return语句。---(不能有返回值,你定义的有void返回值,虽然返回的为空) (3)构造方法的主要作用是完成对象的初始化工作,它能够把定义对象时的参数传给对象的域。 (4)一个类可以定义多个构造方法,如果在定义类时没有定义构造方法,则编译系统会自动插入一个无参数的默认构造方法,这个构造方法不执行任何代码。 (5)构造方法可以重载,以参数的个数,类型,顺序。
Fant_ 2017-09-30
  • 打赏
  • 举报
回复
The constructor is an unusual type of method because it has no return value. This is distinctly 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 这种也不同
Demon_168 2017-09-30
  • 打赏
  • 举报
回复
这些问题自己去看一下书或者资料就能知道。其实我觉得你要自己独立地去找出问题点才能更加有印象。
shangcunshanfu 2017-09-29
  • 打赏
  • 举报
回复
public void A()不是构造函数,只是与类名一样的普通函数 public A()才是构造函数,没有返回直类型
李德胜1995 2017-09-29
  • 打赏
  • 举报
回复
构造函数在哪里???

62,626

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧