java 基础题求解!在线等待!

p10305119 2007-03-30 09:02:33
import java.io.*;
class people{
String name;
int age;
people(){
System.out.println("In people1");
}
people(String name,int age){
this.name=name;
this.age=age;
System.out.println("In people2");
}
}
class student extends people{
String school;
student(){
//super();
this(null,0,null);
System.out.println("In student1");

}
student(String name,int age,String school){
super(name,age);
this.school=school;
System.out.println("In student2");
}
}
class gradute extends student{
String time;
gradute(){
this("a");
System.out.println("In gradute1");
}
gradute(String s){
this.time=s;
System.out.println("In gradute2");
}
}
public class Exam3_16{
public static void main(String args[]){
gradute d=new gradute();
}

}

请问以上程序的输出中为何没有"In people1"?
student(){
//super();
this(null,0,null);
System.out.println("In student1");
}
此构造方法不是隐含调用super()么?应该去调用people()才对!
...全文
299 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
p10305119 2007-03-30
  • 打赏
  • 举报
回复
import java.io.*;
class people{
String name;
int age;
people(){
System.out.println("In people1");
}
people(String name,int age){
this.name=name;
this.age=age;
System.out.println("In people2");
}
}
class student extends people{
String school;
student(){
//super();
this(null,0,null);
System.out.println("In student1");

}
student(String name,int age,String school){
//super(name,age);
this.school=school;
System.out.println("In student2");
}
}
class gradute extends student{
String time;
int n;
String com;
gradute(){
this("a",4,"hua wei");
System.out.println("In gradute1");
}
gradute(String s,int d,String v){
super(s,d,v);
this.time=s;
this.n=d;
this.com=v;
System.out.println("In gradute2");
}
}
public class Exam3_16{
public static void main(String args[]){
gradute d=new gradute();
}

}
p10305119 2007-03-30
  • 打赏
  • 举报
回复
我已经明白了谢谢诸位大师!
tricolors 2007-03-30
  • 打赏
  • 举报
回复
学习。。。
delphi_hk 2007-03-30
  • 打赏
  • 举报
回复
people(String name,int age){
this();
this.name=name;
this.age=age;

System.out.println("this is in people2");
这样写people的带参构造函数,就有" in people1"了.
KevinXie 2007-03-30
  • 打赏
  • 举报
回复
同意 yeah920 的观点

没有显示调用构造函数的时候才会默认调用父类的构造函数.
yiyi2007 2007-03-30
  • 打赏
  • 举报
回复
因为student中是 super(name, age);,所以就调用
People(String name, int age) {
this.name = name;
this.age = age;
System.out.println("In people2");
}
如果改成
Student(String school) {
this.school = school;
System.out.println("In student2");
}

Student()
{
this(null);
System.out.println("In student1");
}

就会显示In people1了
p10305119 2007-03-30
  • 打赏
  • 举报
回复
谢谢各位大师!
yeah920 2007-03-30
  • 打赏
  • 举报
回复
给个实例:
参考上面解释,如果去掉以下这个构造函数,
student(){
//super();
this(null,0,null);
System.out.println("In student1");

}
那么,gradute 这个类将编译不能通过!!!
polarman 2007-03-30
  • 打赏
  • 举报
回复
虚拟机要保证在你没有强制调用同一个类的一个以上构造函数时,只能调用一个构造函数
如果你强制调用了一个构造函数,就不可能自动调用另一个
这里你强制调用了people(String name,int age),所以people()就不被调用
而student的任何一个构造函数你都没有强制调用,所以默认调用student()构造函数
yeah920 2007-03-30
  • 打赏
  • 举报
回复
java里是这样的:
当你有显式的调用父类的构造函数时,就按你的执行;
当没有显式的调用时,就默认调用父类的没有参数的构造函数。

所以,如果你的父类里没有 没有参数的构造函数,而你的子类又不显式的指明调用父类的某一个构造函数,编译就不能通过!!!


不知道明白了没?
p10305119 2007-03-30
  • 打赏
  • 举报
回复
但是为何在
gradute(){}中使用了this("a");却依然调用student();
yeah920 2007-03-30
  • 打赏
  • 举报
回复
student(){
//super();
this(null,0,null);
System.out.println("In student1");
}

如果你这里没有this(null,0,null);这句,那么就象你说的:
构造方法不是隐含调用super();
但是,由于你自己显式的调用了this(null,0,null);
而在student(String name,int age,String school){
super(name,age);
里,又调用了super(name,age);
所以,就有了你看到的结果。

不知道这样解释清楚吗?

62,614

社区成员

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

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