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();
    }
}
相关推荐
鸽鸽程序猿9 分钟前
【JavaEE】【SpringCloud】环境与工程搭建
java·spring cloud·java-ee
我居然是兔子6 小时前
异常练习:在试错中吃透Java异常处理的底层逻辑
java·开发语言
CC.GG7 小时前
【C++】STL容器----unordered_map和unordered_set的使用
java·数据库·c++
Overt0p8 小时前
抽奖系统(4)
java·spring boot·tomcat
想做后端的小C9 小时前
Java:接口回调
java·开发语言·接口回调
爱学习的小可爱卢9 小时前
JavaEE进阶——Spring核心设计模式深度剖析
java·spring·设计模式
毕设源码-钟学长10 小时前
【开题答辩全过程】以 个性化电影推荐网站的设计与实现为例,包含答辩的问题和答案
java·spring boot
C++业余爱好者10 小时前
Power Job 快速搭建 及通信机制介绍
java
qq_27049009610 小时前
SpringBoot药品管理系统设计实现
java·spring boot·后端
、BeYourself11 小时前
SpringAI-ChatClient Fluent API 详解
java·后端·springai