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();
    }
}
相关推荐
特立独行的猫A2 小时前
用仓颉语言 cj-tauri 框架,五分钟给娃做了个鸿蒙形近字游戏
java·后端·python
budingxiaomoli8 小时前
Spring统一功能处理
java·后端·spring
Flynn Liang10 小时前
可观测性与成本治理:一次对话的 17 个观测点
java·langchain·可观测性·micrometer·langchain4j·token 成本·llm 监控
金銀銅鐵10 小时前
带您看懂字节码指令(三):同一行加法,为什么有四种指令
java·jvm
文丑颜不良啊14 小时前
LeetCode2821~2840题目解析
java·leetcode
apcipot_rain15 小时前
C++转Java日记(10)
java·开发语言
简 洁 冬冬16 小时前
泛型方法写法全面辨析:从「能编译」到「能跑」到「能用」
java·泛型
LGL6030A16 小时前
Java学习历程31——初识Spring Boot
java·spring boot·学习
Lansonli18 小时前
大数据Spark(八十八):Spark任务资源之Standalone集群测试
java·服务器·spark
budingxiaomoli18 小时前
Spring AOP
java·spring·java-ee