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();
    }
}
相关推荐
Macbethad2 分钟前
如何用WPF做工控设置界面
java·开发语言·wpf
CodeAmaz9 分钟前
使用责任链模式设计电商下单流程(Java 实战)
java·后端·设计模式·责任链模式·下单
喝养乐多长不高21 分钟前
Rabbit MQ:概述
java·rabbitmq·mq·amqp
拾忆,想起35 分钟前
Dubbo异步调用实战指南:提升微服务并发性能
java·服务器·网络协议·微服务·云原生·架构·dubbo
q***31891 小时前
微服务生态组件之Spring Cloud LoadBalancer详解和源码分析
java·spring cloud·微服务
敏姐的后花园3 小时前
模考倒计时网页版
java·服务器·前端
Dcs5 小时前
Java 中 UnaryOperator 接口与 Lambda 表达式的应用示例
java·后端
bagadesu7 小时前
使用Docker构建Node.js应用的详细指南
java·后端
没有bug.的程序员7 小时前
Spring Cloud Gateway 性能优化与限流设计
java·spring boot·spring·nacos·性能优化·gateway·springcloud