this 关键字的用法
来源:互联网 发布:淘宝小号怎么申请2017 编辑:程序博客网 时间:2024/05/21 10:01
this的作用 一
当方法的参数名和属性名相同的时候,需要用this来做区分,加了this的表示表示属性,没加的表示参数。
public class Student { private String name ; private int age; public Student(String name,int age){ this.name=name; this.age=age; }}
如以上代码所示:当方法的参数名和属性名相同的时候,需要用this来做区分,加了this的表示表示属性,没加的表示参数。若以上构造方法写成下面的样子时是错误的
public Student(String name,int age){ name=name; age=age;}
因为Java 中的就近原则,系统会认为以上两个name和age都是指的参数,所以不会讲参数name和参数age的值赋给属性name和属性age。
this的作用二
this关键字可以调用当前类的构造方法
public class Student { private String name ; private int age; public Student(String name,int age){ this.name=name; this.age=age; } public Student(){ this.("张三",18) }}
当我们在这之后再写以下的代码时
Student S =new Student();
对象S的属性 name不再为NULL age不再为0
name为张三 age为18
this代表谁
我们在初学Java的时候,Java方法中写了this之后却不明白,this代表谁。this代表调用方法的对象。
阅读全文
1 0
- this关键字的用法
- this关键字的用法
- this关键字的用法
- this关键字的用法
- this 关键字的用法
- this 关键字的用法
- this-super关键字的用法
- java this关键字的用法
- 关于 this 关键字的用法
- 关于this关键字的用法
- Java this关键字的用法
- java关键字this的用法
- 关于this关键字的几种用法
- super 和 this 关键字的用法小结
- javascript,jquery中的this关键字的用法
- this 关键字的几种用法
- java this关键字的几种用法
- Java关键字this的用法总结
- vs下的字节对齐的一些疑问
- phpstorm10.0.3与xampp不能post数据 设置方式
- struct2面试
- java基础知识
- Linux Framebuffer驱动框架剖析之一—软件需求
- this 关键字的用法
- Java/JavaWeb/开发环境介绍
- HDU 6060 RXD and dividing (最小斯坦纳树)
- Ubuntu17.04国内源
- react-natvie 开发一个简单的app 流程
- Java8中的Lambda表达式
- ARKit从入门到精通(10)-ARKit让飞机绕着你飞起来
- 基于jquery的组件开发-mask
- DELETE语句