一直学习java,都没有把构造函数弄清楚,通过昨天的学习,现在有了一点点理解,不知道还有什么不知道的,望大家指导指导。
当父类没有任何构造函数,子类定义了一个不带参数的构造函数,这时候在测试类里面创建一个子类的对象时,程序运行时,首先调用系统默认的父类的构造函数,然后再调用子类的构造函数。例子:
public class SuperClass {
PRivate String name;
private String birthdate;
public void show() {
System.out.println("this is a public function" + name + birthdate);
}
}
public class SubClass extends SuperClass {
String address;
public SubClass() {
System.out.println("SubClass:this is a no parameter constructor");
}
public void personinformation(String _name, String _birth, String _address) {
System.out.println("output information" + _name + _birth + _address);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClass absence = new SubClass();
}
}
运行结果:SubClass:this is a no parameter constructor
当父类里面定义了几个构造函数,其中至少有一个不带参数,子类定义了一个不带参数的构造函数,运行的时候,调用不带参数的构造函数,然后再运行子类的构造函数。
public class SuperClass {
private String name;
private String birthdate;
public SuperClass() {
name = "chen";
birthdate = "feb";
System.out.println("SuperClass:this is a no parameter constructor");
}
public SuperClass(String chen, String febu) {
name = chen;
birthdate = febu;
System.out.println("this is parameter constructor:" + name + birthdate);
}
public void show() {
System.out.println("this is a public function" + name + birthdate);
}
}
public class SubClass extends SuperClass {
String address;
public SubClass() {
System.out.println("SubClass:this is a no parameter constructor");
}
public void personinformation(String _name, String _birth, String _address) {
System.out.println("output information" + _name + _birth + _address);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClass absence = new SubClass();
}
}
运行结果:SuperClass:this is a no parameter constructor
SubClass:this is a no parameter constructor
当父类中只定义带参数的构造函数,父类定义的构造函数和上面的一样,这样程序运行就会出现错误,编译不能通过,会出现错误提示。因为在创建子类对象时,由于没有不带参数的构造函数,它不知道要调用父类的那个构造函数。所以要在定义子类构造的第一行加上调用父类的构造函数:super("hhh");根据括号里面的参数类型、个数来确定调用父类里面的那个构造函数。例子:
public class SuperClass {
private String name;
private String birthdate;
public SuperClass(String chen, String febu) {
name = chen;
birthdate = febu;
System.out.println("this is parameter constructor:" + name + birthdate);
}
public SuperClass(String chen) {
name = chen;
System.out.println("this is parameter constructor1:" + name );
}
public void show() {
System.out.println("this is a public function" + name + birthdate);
}
}
public class SubClass extends SuperClass {
String address;
public SubClass() {
super("hhh"); //非常重要!根据他来调用父类的构造函数
System.out.println("SubClass:this is a no parameter constructor");
}
public void personinformation(String _name, String _birth, String _address) {
System.out.println("output information" + _name + _birth + _address);
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClass absence = new SubClass();
}
}
运行结果:this is parameter constructor1:hhh
SubClass:this is a no parameter constructor
父类只有不带参数的构造函数,如果父类里面有一个其他类的对象实例,子类里面同样有一个其他类的对象实例 ,那么运行的时候后,首先运行的是父类里的那个类的构造函数,在运行父类的构造函数,然后运行子类里的那个类的构造函数,最后运行子类的构函数。
public class SuperClass {
private String name;
private String birthdate;
public SuperClass() {
name = "chen";
birthdate = "feb";
Circle2 c2 = new Circle2(); //父类里定义了Circle2的对象c2
System.out.println("SuperClass:this is a no parameter constructor");
}
public SuperClass(String chen, String febu) {
name = chen;
birthdate = febu;
System.out.println("this is parameter constructor:" + name + birthdate);
}
public void show() {
System.out.println("this is a public function" + name + birthdate);
}
}
public class SubClass extends SuperClass {
String address;
Circle c = new Circle(); //父类里定义了Circle的对象c
public SubClass() {
System.out.println("SubClass:this is a no parameter constructor");
}
public void personinformation(String _name, String _birth, String _address) {
System.out.println("output information" + _name + _birth + _address);
}
}
public class Circle {
public static final double PI = 3.14;
public Circle() {
System.out.println("Circle:this is a no parameter constructor");
}
}
public class Circle2 {
public static final double PI = 3.14;
private double banjing;
public Circle2() {
System.out.println("Circle2:this is a no parameter constructor");
}
}
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SubClass absence = new SubClass();
}
}
运行结果:
Circle2:this is a no parameter constructor
SuperClass:this is a no parameter constructor
Circle:this is a no parameter constructor
SubClass:this is a no parameter constructor
新闻热点
疑难解答