举例讲解Java编程中this关键字与super关键字的用法
作者:全速前行 时间:2023-03-09 01:46:02
this
总要有个事物来代表类的当前对象,就像C++中的this指针一样,Java中的this关键字就是代表当前对象的引用。
它有三个主要的作用:
1、在构造方法中调用其他构造方法。
比如有一个Student类,有三个构造函数,某一个构造函数中调用另外构造函数,就要用到this(),而直接使用Student()是不可以的。
2、返回当前对象的引用。
3、区分成员变量名和参数名。
看下面的例子:
public class Student
{
private String name;
private int age;
private String college;
public Student()
{
age = 20;
}
public Student(String name)
{
this();//can not be call Student,only use this() method.
this.name = name;
System.out.println("this student name is "+name);
}
public Student(String name,String college)
{
this(name);//C++中可以直接用Student(name)调用其他构造函数
this.college = college;
System.out.println("this student name is "+name+" college is "+college);
}
public Student upgrade()
{
age++;
return this;
}
public void print()
{
System.out.println("name is: "+name
+" age is: "+age
+" college is: "+college);
}
public static void main(String[] args)
{
Student student1 = new Student("linc");
Student student2 = new Student("linc","shenyang college");
student2.upgrade().print();
}
}
迷失在茫茫的对象海洋时,不要忘了用this来找到自我。
super
super是this的父辈。从面相对象的角度说,这两个概念是很好理解的。
子类从父类继承过来,父类的protected及以上的属性和方法在子类中是天生就具有的。那么,为什么还要有super这个关键字?
第一、看父类的构造
子类构造时要先调用父类的默认构造函数的,这与C++的构造属性一致。当父类有多个构造函数时,你需要指定调用哪个。这是就需要使用super(arg1,arg2...)。
需要注意的是,在子类的构造函数中调用基类的构造函数时,必须要把super写作最前面,否则报错。
第二,在子类覆盖父类的一些方法中再调用父类的此方法。大家都知道,在子类中覆盖父类的一些方法是面向对象中多态的一种方式,而因为其他种种原因,需要在此方法中调用父类的此方法,用以区分,此时需要使用super来完成。
public class ClassLeader extends Student
{
private String duty;
public ClassLeader()
{
duty = "class monitor";
}
public ClassLeader(String duty,String name,String college)
{
super(name,college);
this.duty = duty;
}
public void print()
{
super.print();
System.out.println("duty is " + duty);
}
public static void main(String[] args)
{
ClassLeader leader = new ClassLeader("life","linc","shenyang");
leader.print();
}
}
将两个类文件放在同一个目录,编译并运行:
D:\workspace\Java\project261\super>javac -d . *java
D:\workspace\Java\project261\super>java ClassLeader
运行结果:
this student name is linc
this student name is linc college is shenyang
name is: linc age is: 20 college is: shenyang
duty is life
看看在其他语言中是怎样来处理的:
C#中提供了base关键字来完成super相似的功能,C++直接用基类的名字来调用。
标签:Java,this,super
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 算法之归并排序详解及实现代码
2021-12-27 01:52:11
详解java一维数组及练习题实例
2023-05-21 17:31:56
springboot 实战:异常与重定向问题
2022-03-06 15:44:54
![](https://img.aspxhome.com/file/2023/8/66048_0s.png)
详解spring boot集成RabbitMQ
2022-06-25 17:56:07
在Mybatis中使用自定义缓存ehcache的方法
2022-02-24 17:27:35
Android实现未读消息小红点显示实例
2022-05-18 07:57:27
![](https://img.aspxhome.com/file/2023/0/117140_0s.jpg)
Unity3D仿写Button面板事件绑定功能
2023-03-14 10:38:14
![](https://img.aspxhome.com/file/2023/6/129626_0s.png)
你所不知道的Spring自动注入详解
2021-09-04 19:30:08
![](https://img.aspxhome.com/file/2023/8/60828_0s.png)
Java Socket编程(四) 重复和并发服务器
2022-06-29 07:07:06
MyBatis-Plus拦截器实现数据权限控制的示例
2022-12-10 05:10:17
Java求一个分数数列的前20项之和的实现代码
2021-08-22 14:59:58
Java @Deprecated注解的作用及传递性
2023-08-11 12:55:05
![](https://img.aspxhome.com/file/2023/7/58097_0s.png)
Java 格式化输出JSON字符串的2种实现操作
2023-11-13 09:41:10
Android自定义View倒计时圆
2022-01-06 17:42:30
![](https://img.aspxhome.com/file/2023/1/126731_0s.jpg)
深入剖析构建JSON字符串的三种方式(推荐)
2023-09-26 07:47:22
![](https://img.aspxhome.com/file/2023/6/68696_0s.png)
解析Java内存分配和回收策略以及MinorGC、MajorGC、FullGC
2023-02-06 08:22:19
![](https://img.aspxhome.com/file/2023/3/62783_0s.png)
Centos中安装jdk案例讲解
2023-04-30 00:37:50
![](https://img.aspxhome.com/file/2023/5/74975_0s.png)
解决Springboot项目启动后自动创建多表关联的数据库与表的方案
2023-11-24 01:11:27
![](https://img.aspxhome.com/file/2023/9/59719_0s.jpg)
基于C#技术实现身份证识别功能
2023-10-01 14:16:26
![](https://img.aspxhome.com/file/2023/8/91358_0s.png)
Kotlin标准函数与静态方法应用详解
2022-01-21 10:45:33