博客
关于我
java —— this 关键字
阅读量:289 次
发布时间:2019-03-03

本文共 605 字,大约阅读时间需要 2 分钟。

@

什么时候用this?

在程序产生二义性的时候,用 this 来指明当前对象;

  • 普通方法:this 指调用该方法的对象
  • 构造方法:this 指要初始化的对象

注意!!!

this 不能用于 static 方法中

只是看概念的话还是不行

撸代码:

/** * @author DREAM_yao * this关键字的用法 *//*this:创建好的当前对象对象的地址。 * 构造方法:指向要初始化的对象 *  * 普通方法:指向调用该方法的对象 * */class Score{   	int t1,t2;	public Score(int t1) {   		this.t1=t1;		/*必须位于构造方法的第一句*/	}	public Score(int t1,int t2){   		this.t1=t1;		this.t2=t2;/*构造方法*/	}	public void getSum() {   /*普通方法*/		System.out.println(this.t1+this.t2);	}}public class TestConstructor {   	public static void main(String[] args) {   		Score s1 = new Score(10);		Score s2 = new Score(5,15);		s2.getSum();	}}

转载地址:http://whsl.baihongyu.com/

你可能感兴趣的文章
Nginx开启gzip网页传输压缩配置
查看>>
nginx开机启动脚本
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
Nginx搭建RTMP服务器+FFmpeg实现海康威视摄像头预览
查看>>
Nginx搭建静态资源映射实现远程访问服务器上的图片资源
查看>>
nginx日志不支持中文
查看>>
nginx日志分割并定期删除
查看>>
Nginx日志分析系统---ElasticStack(ELK)工作笔记001
查看>>
Nginx日志按天分割
查看>>
Nginx映射本地json文件,配置解决浏览器跨域问题,提供前端get请求模拟数据
查看>>
Nginx映射本地静态资源时,浏览器提示跨域问题解决
查看>>
nginx最最最详细教程来了
查看>>
Nginx服务器---正向代理
查看>>
Nginx服务器上安装SSL证书
查看>>
Nginx服务器基本配置
查看>>
Nginx服务器的安装
查看>>
Nginx架构详解
查看>>