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();
    }
}
相关推荐
karry_k11 小时前
MyBatis批量insert-select踩坑:useGeneratedKeys=true 可能让PostgreSQL返回大量插入结果
java·后端
karry_k11 小时前
PostgreSQL 在 MyBatis 中执行正常 SQL 失效:一次 DELETE USING 踩坑记录
java·后端
SamDeepThinking14 小时前
从源码到代码:MyBatis-Flex 与 MyBatis-Plus 的逐项对比
java·后端·程序员
她的男孩17 小时前
Spring Boot 接 Flowable 工作流:用 3 个注解搭一个请假审批流程
java·后端·架构
荣码19 小时前
LLM结构化输出:让AI返回JSON而不是废话,我踩了4个坑
java·python
plainGeekDev20 小时前
Gson → kotlinx.serialization
android·java·kotlin
小bo波1 天前
Java Swing 图形用户界面实验 —— 从算术练习到游戏开发的完整实践
java·课程设计·gui·游戏开发·扫雷·swing
咖啡八杯1 天前
GoF设计模式——备忘录模式
java·后端·spring·设计模式