Java super关键字的使用详解
作者:世界尽头与你 时间:2021-11-30 13:40:43
1.super介绍
我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。
用于访问父类的属性,方法,构造器
2.super的基本使用
我们来演示以下子类调用父类的属性,方法和构造器
父类:
/**
* super关键字演示父类
*/
public class SuperFather {
public int n1 = 100;
int n2 = 110;
protected int n3 = 120;
private int n4 = 130;
public void say() {
System.out.println("我是公开的方法");
}
protected void show() {
System.out.println("我是受保护的方法");
}
private void can() {
System.out.println("我是私有方法");
}
}
子类:
/**
* super关键字演示子类
*/
public class SuperSon extends SuperFather {
public void test() {
// 访问父类的属性,不能访问父类的私有属性
System.out.println(super.n1 + super.n2 + super.n3);
// 访问父类的方法,同样不能访问父类的私有方法
super.say();
super.show();
}
public SuperSon() {
// 访问父类的构造器
// 访问无参构造
super();
}
}
3.子类和父类方法的调用细节
子类访问父类的方法,子类与父类方法无重名:
父类:
public void sum() {
System.out.println("我是sum功能!");
}
子类:
// 本类中没有sum方法,则去父类中找sum方法,父类没有则找爷爷类
sum();
super.sum(); // 通过这种方法也可以访问哦,这种方法会跳过本类的查找直接在父类中查找
子类访问父类的方法,子类与父类方法有重名:
这种情况,子类想要访问父类的方法,只能通过super关键字来访问:
父类:
public void cal() {
System.out.println("我是父类的cal功能 !");
}
子类:
public void cal() {
System.out.println("我是子类的cal功能!");
}
public void test() {
// 子类和父类重名的方法
super.cal();
}
属性的访问规则同方法
4.super和this的区别
super关键字:我们可以通过super关键字来实现对父类成员的访问,用来引用当前对象的父类。
this关键字:指向自己的引用。
下面用代码直观的展示下二者的区别
// 父类
public class Person {
public String name = "小卤蛋";
public void print(){
System.out.println("Person");
}
}
// 子类
public class Student extends Person{
private String name = "鲁班七号";
public void test(String name){ // 输出name的方法
System.out.println(name);
System.out.println(this.name);
System.out.println(super.name);
}
public void print(){
System.out.println("Student");
}
public void test2(){ // 输出方法
print();
this.print();
super.print();
}
}
// main方法
public static void main(String[] args) {
Student student = new Student();
student.test("鲁班七号");
student.test2();
}
来源:https://blog.csdn.net/Gherbirthday0916/article/details/125732828
标签:Java,super,关键字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
![](https://img.aspxhome.com/file/2023/4/103104_0s.png)
Java 用反射设置对象的属性值实例详解
2023-11-26 03:56:51
ReadWriteLock接口及其实现ReentrantReadWriteLock方法
2023-11-24 01:46:52
![](https://img.aspxhome.com/file/2023/3/59873_0s.png)
elasticsearch集群发现zendiscovery的Ping机制分析
2021-05-25 05:40:55
![](https://img.aspxhome.com/file/2023/2/60572_0s.jpg)
Flutter Widgets之标签类控件Chip详解
2023-06-26 14:22:35
![](https://img.aspxhome.com/file/2023/0/120090_0s.jpg)
一文带你搞懂Java8的LocalDateTime
2023-11-10 02:02:22
![](https://img.aspxhome.com/file/2023/4/59234_0s.png)
java实现哈弗曼编码与反编码实例分享(哈弗曼算法)
2023-11-25 04:54:05
java selenium教程环境搭建基于Maven
2023-11-27 01:35:38
SpringBoot集成Mybatis-plus并实现自动生成相关文件的示例代码
2023-08-15 04:12:46
Mybatis核心组成部分之SQL映射文件揭秘详解
2023-08-22 18:45:21
Java守护线程实例详解_动力节点Java学院整理
2023-03-29 08:14:30
![](https://img.aspxhome.com/file/2023/5/60175_0s.png)
Spring Boot使用Allatori代码混淆的方法
2023-11-24 16:34:55
![](https://img.aspxhome.com/file/2023/0/59080_0s.jpg)
Java编程泛型限定代码分享
2023-11-09 17:46:32
java实现单词搜索迷宫游戏
2023-11-10 22:44:32
Java设计模式之命令模式
2022-06-17 22:49:07
flyway实现java 自动升级SQL脚本的问题及解决方法
2021-10-14 00:23:19
![](https://img.aspxhome.com/file/2023/9/60399_0s.jpg)
Intellij搭建springmvc常见问题解决方案
2023-07-23 12:53:29
![](https://img.aspxhome.com/file/2023/9/57419_0s.png)
Spring Boot ActiveMQ连接池配置过程解析
2023-11-08 23:08:02
详解如何在Java中实现堆排序算法
2023-11-11 11:34:46
Android ListView的Item点击效果的定制
2023-06-21 11:46:01