Java 学习----子类构造器 调用 父类构造器

class Father
{
	String name;
	int age;

	public Father()
	{
		//default
		System.out.println("Father constructor default");
	}

	public  Father(int age)
	{
		this.age=age;
		System.out.println("Father constructor 11111");
	}

	public Father(int age ,String name)
	{
		this(age);
		this.name=name;
		System.out.println("Father constructor 22222");
	}
}

class Son extends Father
{
	public Son()
	{
		System.out.println("Son constructor default");
		//default
	}
	public Son(int age, String name)
	{	
		super(age,name);
		System.out.println("Son constructor 11111");
	}
}



public class Test 
{
	public static void main(String[] args) 
	{
		Son two=new Son();
		System.out.println("====================================================");
		Son three=new Son(12,"three");
	}
}


result;


---------- JAVA_RUN ----------
Father constructor default
Son constructor default
====================================================
Father constructor 11111
Father constructor 22222
Son constructor 11111

Output completed (1 sec consumed) - Normal Termination

你可能感兴趣的:(Java 学习----子类构造器 调用 父类构造器)