java 继承

继承

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()去调用父类中的构造方法。并且当你给他加上一个参数时,他会调用父类中的有参构造

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值