Java中this关键字的用法详解
作者:赖东东很不错 时间:2023-10-04 05:05:53
一、this关键字
1.this的类型:哪个对象调用就是哪个对象的引用类型
二、用法总结
1.this.data; //访问属性
2.this.func(); //访问方法
3.this(); //调用本类中其他构造方法
三、解释用法
1.this.data
这种是在成员方法中使用
让我们来看看不加this会出现什么样的状况
class MyDate{
public int year;
public int month;
public int day;
public void setDate(int year, int month,int day){
year = year;//这里没有加this
month = month;//这里没有加this
day = day;//这里没有加this
}
public void PrintDate(){
System.out.println(year+"年 "+month+"月 "+day+"日 ");
}
}
public class TestDemo {
public static void main(String[] args) {
MyDate myDate = new MyDate();
myDate.setDate(2000,9,25);
myDate.PrintDate();
MyDate myDate1 = new MyDate();
myDate1.setDate(2002,7,14);
myDate1.PrintDate();
}
}
我们想要达到的预期是分别输出2000年9月25日,2002年7月14日。
而实际输出的结果是
而当我们加上this时
class MyDate{
public int year;
public int month;
public int day;
public void setDate(int year, int month,int day){
this.year = year;
this.month = month;
this.day = day;
}
public void PrintDate(){
System.out.println(this.year+"年 "+this.month+"月 "+this.day+"日 ");
}
}
public class TestDemo {
public static void main(String[] args) {
MyDate myDate = new MyDate();
myDate.setDate(2000,9,25);
myDate.PrintDate();
MyDate myDate1 = new MyDate();
myDate1.setDate(2002,7,14);
myDate1.PrintDate();
}
}
就实现了赋值的功能,为了避免出现差错,我们建议尽量带上this
2.this.func()
这种是指在普通成员方法中使用this调用另一个成员方法
class Student{
public String name;
public void doClass(){
System.out.println(name+"上课");
this.doHomeWork();
}
public void doHomeWork(){
System.out.println(name+"正在写作业");
}
}
public class TestDemo2 {
public static void main(String[] args) {
Student student = new Student();
student.name = "小明";
student.doClass();
}
}
运行结果:
3.this()
这种指在构造方法中使用this调用本类其他的构造方法
这种this的使用注意以下几点
1.this只能在构造方法中调用其他构造方法
2.this要放在第一行
3.一个构造方法中只能调用一个构造方法
运行结果
来源:https://blog.csdn.net/benxiangsj/article/details/124282800
标签:java,this,关键字
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot拦截器的使用
2023-04-27 23:27:27
![](https://img.aspxhome.com/file/2023/7/91077_0s.png)
Android 使用maven publish插件发布产物(aar)流程实践
2023-03-04 18:07:45
![](https://img.aspxhome.com/file/2023/7/138977_0s.png)
深入学习C#网络编程之HTTP应用编程(上)
2023-12-12 23:12:27
![](https://img.aspxhome.com/file/2023/4/89834_0s.png)
Android实现纸飞机的简单操作
2022-05-16 12:26:04
VC++时钟函数
2021-06-17 10:07:51
C#中字符串与字节数组的转换方式
2023-06-28 10:43:53
Java之单例模式实现方案详解
2022-02-15 19:02:29
C#判断一个矩阵是否为对称矩阵及反称矩阵的方法
2023-04-04 10:55:23
![](https://img.aspxhome.com/file/2023/4/108744_0s.png)
Springmvc Controller接口代码示例
2023-11-28 10:13:25
Spring Cloud Hystrix 线程池队列配置(踩坑)
2023-12-18 16:13:22
![](https://img.aspxhome.com/file/2023/3/130013_0s.jpg)
在C#中使用Channels的完整教程
2021-11-03 15:41:28
Springboot自动装配实现过程代码实例
2023-11-14 19:50:19
![](https://img.aspxhome.com/file/2023/3/69303_0s.png)
从零开始学springboot整合feign跨服务调用的方法
2023-05-15 18:30:22
![](https://img.aspxhome.com/file/2023/5/81895_0s.png)
Android布局之帧布局FrameLayout详解
2023-08-07 04:45:29
![](https://img.aspxhome.com/file/2023/5/85985_0s.png)
使用okhttp替换Feign默认Client的操作
2021-10-03 16:57:59
![](https://img.aspxhome.com/file/2023/6/70536_0s.jpg)
Android布局之GridLayout网格布局
2022-04-24 22:49:46
![](https://img.aspxhome.com/file/2023/8/137958_0s.png)
Android实现本地Service方法控制音乐播放
2021-12-16 07:22:31
![](https://img.aspxhome.com/file/2023/0/129140_0s.jpg)
IDEA启动tomcat控制台中文乱码问题的解决方法(100%有效)
2021-06-25 10:45:23
![](https://img.aspxhome.com/file/2023/9/62699_0s.jpg)
Spring boot外部配置(配置中心化)详解
2022-07-11 23:13:26
Spring boot多线程配置方法
2022-12-14 21:02:02
![](https://img.aspxhome.com/file/2023/9/122269_0s.png)