JAVA中this关键字的使用
来源:互联网 发布:游族网络定增价格2017 编辑:程序博客网 时间:2024/05/22 16:46
1.为了解决可读性的问题,通过this关键字可以明确的访问一个类的成员变量,解决与局部变量命名的问题,写一个小的例子。
eg:
class Person{ int age; public Person(int age) { this.age=age; } public getAge() { return this.age; }}
2.this关键字可以调用成员方法
eg
class Person{ public void openMouth() { ... } public void speak() { this.openMouth(); }}
3.构造方法是在实例化对象时被JAVA虚拟机自动调用的,在程序中不同像调用其他方法一样去调用构造方法,但是可以在一个构造方法中使用”this ([参数1,参数2…])”的形式调用其他的构造方法。
eg
class Person(){ public Person() { System.out.println("调用无参构造方法"); } public Person(String name) { this(); System.out.println("调用有参的构造方法") }}public class Example{ public static void main() { Person person = new Person("liming"); }}
但是使用this调用其他构造方法,不能在成员方法中调用并且不能在一个类的两个构造方法中使用this互相调用。同时在构造方法中,使用this调用构造方法的语句必须位于第一行,有且只能出现一次。
阅读全文
0 0
- java中this关键字的使用
- java中this关键字的使用
- Java中this关键字的使用
- java中this关键字的使用
- Java中this关键字的使用
- Java中 “this” 关键字的使用
- java中this关键字的使用
- java中this关键字的使用
- JAVA中this关键字的使用
- java中this关键字的使用(code)
- java中this关键字的基本使用
- JAVA中“this”关键字的使用
- Java中this关键字和super关键字的使用
- java中this关键字使用
- JAVA中this关键字的
- JAVA this关键字的使用
- java this关键字的使用
- java关键字this的使用
- 矩阵快速幂之斐波那契数列的前1000,000,000项mod10000
- Web Service简要介绍
- C++------输入输出和文件控制
- ESLint与EditorConfig
- LDD3源码分析之与硬件通信&中断处理
- JAVA中this关键字的使用
- C++格式化输出各种符号的含义
- CSRF攻击即防护
- Mysql数据库的备份
- 1050. 螺旋矩阵(25) PAT
- BLAT 安装
- 数据仓库与数据集市建模
- vsftp上传文件出现553 Could not create file
- 文章标题