`
李灵晖-raylee
  • 浏览: 128626 次
博客专栏
Group-logo
从头认识java
浏览量:0
文章分类
社区版块
存档分类
最新评论

从头认识java-4.4 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的四个注意点,以及注意点在编码中的实现。


2.this

2.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.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) {
	}
}

2.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()只能引用构造器方法。


2.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 的四个方法,以及在编码中的实现。

这一章节就到这里,谢谢。

-----------------------------------

目录


版权声明:本文为博主原创文章,未经博主允许不得转载。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics