从头认识java-4.4 this
2015-10-26 09:03
639 查看
这一章节我们来讨论一些this
1 this是在类的内部使用,它指向对象的引用。
上面代码里面的this就是返回Test这个类型的对象。
this还可以操作方法:
method say
上面的代码通过引用Test的对象,操作say这个方法。
其实,在类的内部使不使用this倒无所谓,只不过增加了可读性,下面的代码为我们展示这个:
输出:
method say
method say
从输出结果可以看出,其实有没有this,都是可以调用内部方法。
2 this可以更好区分参数与属性域。
我们在编码的时候,使用很多参数的名称都跟属性域的相同,因此,使用this让我们更好的区分两者。
例如:
上面的代码非常难区分两个id究竟赋值是怎样的,因此,我们在这里加上this,让代码更好理解。
3 this可以引用之前的构造器。
输出:
created Test
id:1
name:jack
由于使用了this,因此使得代码更加间接。
注意:
(1)this()引用的构造器必须放在第一句,不然会报错。
(2)this()引用的构造器不能同时使用两个以上。
(3)this只能引用普通方法,而this()只能引用构造器方法。
4 this不能引用static方法
有static的方法是类方法,它不需要创建对象即可使用,但是这种方法不能够被this引用。
总结:这一章节我们主要讨论了this的四个注意点,以及注意点在编码中的实现。
1 this是在类的内部使用,它指向对象的引用。
package com.ray.ch01; public class Test { private Test getTest() { return this; } public static void main(String[] args) { } }
上面代码里面的this就是返回Test这个类型的对象。
this还可以操作方法:
package com.ray.ch01; public class Test { private void say() { System.out.println("method say"); } private void testSay() { this.say(); } public static void main(String[] args) { new Test().testSay(); } }输出:
method say
上面的代码通过引用Test的对象,操作say这个方法。
其实,在类的内部使不使用this倒无所谓,只不过增加了可读性,下面的代码为我们展示这个:
package com.ray.ch01; public class Test { private void say() { System.out.println("method say"); } private void testSay1() { this.say(); } private void testSay2() { say(); } public static void main(String[] args) { new Test().testSay1(); new Test().testSay2(); } }
输出:
method say
method say
从输出结果可以看出,其实有没有this,都是可以调用内部方法。
2 this可以更好区分参数与属性域。
我们在编码的时候,使用很多参数的名称都跟属性域的相同,因此,使用this让我们更好的区分两者。
例如:
package com.ray.ch01; public class Test { private int id; public Test(int id) { id = id; } public static void main(String[] args) { } }
上面的代码非常难区分两个id究竟赋值是怎样的,因此,我们在这里加上this,让代码更好理解。
package com.ray.ch01; public class Test { private int id; public Test(int id) { this.id = id; } public static void main(String[] args) { } }
3 this可以引用之前的构造器。
package com.ray.ch01; public class Test { private int id; private String name; public Test() { System.out.println("created Test"); } public Test(int id) { this(); this.id = id; System.out.println("id:" + id); } public Test(int id, String name) { this(id); this.name = name; System.out.println("name:" + name); } public static void main(String[] args) { new Test(1, "jack"); } }
输出:
created Test
id:1
name:jack
由于使用了this,因此使得代码更加间接。
注意:
(1)this()引用的构造器必须放在第一句,不然会报错。
(2)this()引用的构造器不能同时使用两个以上。
(3)this只能引用普通方法,而this()只能引用构造器方法。
4 this不能引用static方法
package com.ray.ch01; public class Test { private static void say() { System.out.println("method say"); } public static void main(String[] args) { Test.say(); } }
有static的方法是类方法,它不需要创建对象即可使用,但是这种方法不能够被this引用。
总结:这一章节我们主要讨论了this的四个注意点,以及注意点在编码中的实现。
相关文章推荐
- java IO流
- 从头认识java-4.3 默认构造器
- 从头认识java-4.3 默认构造器
- Java解析XML格式数据的方法详解
- java中sleep()和wait()的区别
- 表单的struts标签示例
- java线程同步方法
- Java内存分析一
- Java事务的深入理解 JAT
- [转]Eclipse 项目转移到Android Studio遇到的问题
- spring mvc处理流程概述
- [深入理解Java虚拟机]第八章 字节码执行引擎-基于栈的字节码解释执行引擎
- eclipse快捷键以及使用技巧大全
- Spring AOP 实现原理与 CGLIB 应用
- Java 中正确使用 hashCode 和 equals 方法
- 用java实现欧几里得算法求两个数字的最大公约数
- java枚举类型enum的使用
- java中HashMap详解
- Java TreeMap 源码解析
- [深入理解Java虚拟机]第八章 字节码执行引擎-方法调用