java继承——super关键字的使用

复制代码
super注意点:
     1.super调用父类的构造方法,必须在构造方法的第一个
     2.super 必须只能出现在子类的方法或者构造方法中!
     3.super和this 不能同时调用构造方法!
     4.私有的东西不能被直接继承,只能被间接继承
     (创建一个构造函数的快捷键:Alt+int)

 Vs this:
      代表的对象不同:
           this:   本身调用者这个对象
           super:  代表父类对象的应用
      前提
           this:   没有继承也可以使用
           super:  只能继承条件才可以使用
      构造方法:
          this():本类的构造
          super():父类的构造!

代码案例:

java 复制代码
//父类: 
public class Person {
protected String name = "qingchen";
    public void print(){
        System.out.println("Person");
    }
}
//子类
public class Student extends Person {
private String name = "chen";
    public void print(){
        System.out.println("Student");
    }
    public void test1(){
        print();//Student
        this.print();//Student
        super.print();//Person
    }
    public void test(String name){
        System.out.println(name);
        System.out.println(this.name);
        System.out.println(super.name);//super指向父类
    }
}
//输出:
public class Application {

 Student student =new Student();
        student.test("清宸");
        student.test1();
    }
}
相关推荐
xuhaoyu_cpp_java31 分钟前
过滤器与监听器学习
java·经验分享·笔记·学习
程序员小假1 小时前
我们来说一下 b+ 树与 b 树的区别
java·后端
Meepo_haha2 小时前
Spring Boot 条件注解:@ConditionalOnProperty 完全解析
java·spring boot·后端
sheji34162 小时前
【开题答辩全过程】以 基于springboot的房屋租赁系统的设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
木井巳2 小时前
【递归算法】子集
java·算法·leetcode·决策树·深度优先
行百里er3 小时前
优雅应对异常,从“try-catch堆砌”到“设计驱动”
java·后端·代码规范
ms_27_data_develop3 小时前
Java枚举类、异常、常用类
java·开发语言
xiaohe073 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
代码飞天3 小时前
wireshark的高级使用
android·java·wireshark