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();
    }
}
相关推荐
五岳4 小时前
分库分表数据源ShardingSphereDataSource的Connection元数据误用问题分析
java·mysql·爬坑
带刺的坐椅4 小时前
迈向 MCP 集群化:Solon AI (支持 Java8+)在解决 MCP 服务可扩展性上的探索与实践
java·ai·llm·solon·mcp
鼠爷ねずみ4 小时前
SpringCloud前后端整体开发流程-以及技术总结文章实时更新中
java·数据库·后端·spring·spring cloud
代码or搬砖4 小时前
String字符串
android·java·开发语言
AM越.6 小时前
Java设计模式详解--装饰器设计模式(含uml图)
java·设计模式·uml
5980354156 小时前
【java工具类】小数、整数转中文大写
android·java·开发语言
JIngJaneIL6 小时前
基于java + vue个人博客系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot
吃喝不愁霸王餐APP开发者7 小时前
Java后端服务在对接全国性霸王餐API时的多数据中心部署与就近调用策略
java·开发语言
从心归零7 小时前
springboot-jpa的批量更新方法
java·spring boot·spring