随笔 - 54  文章 - 0  评论 - 0  阅读 - 3832

继承-------Super

super注意点:

1.super调用父类的构造方法,必须在构造方法的第一个

2.super 必须只能出现在子类的方法或者构造方法中!

3.super 和this 不能同时调用构造方法!

复制代码
public class Person {
    //父类的无参构造
    public Person(){
        System.out.println("爸爸来啦");
    }
    
    String name = "李航";
    private int age = 10;//私有属性子类无法调用
    
    public void say(){
        System.out.println("我是李航啊");
    }
}


public class Student extends Person {
    //无参构造,先执行父类的无参构造,再执行子类的无参构造
    public Student() {
        super();//继承父类的无参构造
        System.out.println("儿砸来啦");
    }

    //定义子类属性name 和方法say()
    String name = "娣娣";
    public void say(){
        System.out.println("我是娣娣呀");
    }


    //test1----- super调用父类的属性,this 调用子类的属性
    public void test1 (String name){
        System.out.println(name);//这里的name指的是test1方法中的输入name
        System.out.println(this.name);
        System.out.println(super.name);
    }

    //test2----super调用父类的方法,this调用子类的方法
    public void test2(){
        say();
        this.say();
        super.say();
    }
}

/*
public class Amplication {
    public static void main(String[] args) {

        Student student = new Student();

        student.test1("点点");
        student.test2();
    }
}
*/
复制代码

 

VS this :

代表的对象不同:

this :本身调用者这个对象

super :代表父类对象的应用

前提: this:没有继承也可以使用

super:只能在继承条件才可以使用

构造方法:

this() : 本类的构造

super():父类的构造

 

posted on   三岁学JAVA  阅读(34)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示