java中的子类和父类问题(补充)
2012-07-23 12:16
344 查看
publicclass Person {
private String
name;
private Integer
age;
public Person() {
super();
this.name =
null;
this.age = -1;
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
}
publicclass Student
extends Person{
private String
id;
public Student() {
super();
}
public Student(String id) {
super();
this.id = id;
}
}
//如果将person()的无参构造方法删掉.则会报习异常.是因为在子类student中super() --->就是掉用的是父类person的无参构造方法.
子类student不写super() jvm也会默认的调用父类的无参构造.但有一种情况不会报错.在子类student中的构造函数,显式的调用父类的有参构造函数,则父类中就不用写无参的构造函数了.
private String
name;
private Integer
age;
public Person() {
super();
this.name =
null;
this.age = -1;
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
}
publicclass Student
extends Person{
private String
id;
public Student() {
super();
}
public Student(String id) {
super();
this.id = id;
}
}
//如果将person()的无参构造方法删掉.则会报习异常.是因为在子类student中super() --->就是掉用的是父类person的无参构造方法.
子类student不写super() jvm也会默认的调用父类的无参构造.但有一种情况不会报错.在子类student中的构造函数,显式的调用父类的有参构造函数,则父类中就不用写无参的构造函数了.
相关文章推荐
- java中的子类和父类问题(补充)
- java中子类重写父类方法要注意的问题
- Java____父类、子类——构造代码块、方法调用顺序问题
- java中的继承-子类继承父类的权限问题
- java中子类继承父类程序执行顺序问题
- Java关于子类对父类私有方法覆盖的问题
- Java里子类调用父类构造方法问题
- Java基础重温(六)子类与父类实例方法、静态方法的重写问题
- Java里子类调用父类构造方法问题
- Java学习之子类与父类实例化问题~
- java中的子类和父类问题
- java中子类继承父类程序执行顺序问题
- java中子类继承自父类的权限问题
- Java子类重写父类方法注意问题收集(转)
- [转]Java中子类调用父类构造方法的问题分析
- java类继承中父类调用子类函数的问题
- java继承,多态方面的一个问题,定义一个父类对象,用子类new
- Java中关于子类覆盖父类的抛出异常问题
- java 中关于子类调用父类构造函数问题
- Java里子类调用父类构造方法问题