博客
关于我
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 + Spring Boot 实现负载均衡
查看>>
Nginx + Tomcat + SpringBoot 部署项目
查看>>
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
nginx - thinkphp 如何实现url的rewrite
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
Nginx - 反向代理与负载均衡
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx 301跳转
查看>>
nginx 403 forbidden
查看>>
nginx connect 模块安装以及配置
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
nginx http配置说明,逐渐完善。
查看>>
Nginx keepalived一主一从高可用,手把手带你一步一步配置!
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>
Nginx Lua install
查看>>