继承
1.通过关键词extends继承一个已有的类,被继承的类叫做父类(超类),继承的类叫做子类(派生类),
package com.zhy.parent; package com.zhy.parent; public class Person { public Person(){ System.out.println("这是父类的无参构造"); } public Person(int n){ System.out.println("这是父类的有参构造"); } public void Own(){ System.out.println("我爱瓜瓜小朋友"); } } package com.zhy.parent; public class Student extends Person{ } package com.zhy.parent; public class Test { public static void main(String[] args) { Student t1=new Student(); t1.Own(); } } //结果: //这是父类的无参构造 //我爱瓜瓜小朋友
根据以上代码可以显现的看出父类中的无参构造方法和Own()在子类中存在。
package com.zhy.parent; package com.zhy.parent; public class Person { public Person(){ System.out.println("这是父类的无参构造"); } public Person(int n){ System.out.println("这是父类的有参构造"); } public void Own(){ System.out.println("我爱瓜瓜小朋友"); } } package com.zhy.parent; public class Student extends Person{ public Student(){ System.out.println("这是子类的无参构造"); } } package com.zhy.parent; public class Test { public static void main(String[] args) { Student t1=new Student(); t1.Own(); } } //这是父类的无参构造 //这是子类的无参构造 //我爱瓜瓜小朋友
在这一份代码中我们定义了子类的无参构造方法,但没有去调用父类的无参,然而在测试类中父类无参也得到了执行。
所以得到了一个结论:在子类的无参构造方法中有一个隐藏的父类的无参构造方法,并且这个父类的无参方法一定在子类无参的第一行。
我们继续往下证明:
子类中定义有参构造,没有无参构造。
package com.zhy.parent; public class Person { public Person(){ System.out.println("这是父类的无参构造"); } public Person(int n){ System.out.println("这是父类的有参构造"); } public void Own(){ System.out.println("我爱瓜瓜小朋友"); } } package com.zhy.parent; public class Student extends Person{ public Student(int n){ System.out.println("这是子类的有参构造"); } } package com.zhy.parent; public class Test { public static void main(String[] args) { Student t1=new Student(1); t1.Own(); } } //这是父类的无参构造 //这是子类的有参构造 //我爱瓜瓜小朋友
父类中没有无参构造
package com.zhy.parent; public class Person { public Person(int n){ System.out.println("这是父类的有参构造"); } public void Own(){ System.out.println("我爱瓜瓜小朋友"); } } package com.zhy.parent; public class Student extends Person{ public Student(int n){ System.out.println("这是子类的有参构造"); } } package com.zhy.parent; public class Test { public static void main(String[] args) { Student t1=new Student(1); t1.Own(); } } //java: 无法将类 com.zhy.parent.Person中的构造器 Person应用到给定类型; //需要: int //找到: 没有参数 //原因: 实际参数列表和形式参数列表长度不同
所以,我们可以懂得在子类的构造方法中,一定存在一个隐藏了的父类无参构造方法。
这个也就是super的用法
在第一份代码中我加上super。
package com.zhy.parent; package com.zhy.parent; public class Person { public Person(){ System.out.println("这是父类的无参构造"); } public Person(int n){ System.out.println("这是父类的有参构造"); } public void Own(){ System.out.println("我爱瓜瓜小朋友"); } } package com.zhy.parent; package com.zhy.parent; public class Student extends Person{ public Student(){ super(); } } package com.zhy.parent; public class Test { public static void main(String[] args) { Student t1=new Student(); t1.Own(); } } //结果: //这是父类的无参构造 //我爱瓜瓜小朋友
我们可以发现这两份代码的结果是一样的
那么有参呢
package com.zhy.parent; package com.zhy.parent; public class Person { public Person(){ System.out.println("这是父类的无参构造"); } public Person(int n){ System.out.println("这是父类的有参构造"); } public void Own(){ System.out.println("我爱瓜瓜小朋友"); } } package com.zhy.parent; public class Student extends Person{ public Student(int n){ super(1); } } package com.zhy.parent; public class Test { public static void main(String[] args) { Student t1=new Student(1); t1.Own(); } } //这是父类的有参构造 //我爱瓜瓜小朋友
所以在子类的构造方法中存在一个super()去调用父类中的构造方法。并且当你给他加上一个参数时,他会调用父类中的有参构造