---
title: "this 关键字的作用"
author: "hellojim"
date: "2009-01-02T16:42:48Z"
tags: []
slug: "this"
draft: false
lastmod: "2009-01-02T21:05:44Z"
id: 308935
url: "https://hellojim.iteye.com/blog/308935"
---

```
//此类说明了 this 关键字的作用
/*  1.用在构造方法内部 
  2.调用某个构造方法
  3.在方法内部要得到调用本方法的那个对象(此种情况 this 关键字是可以省略的)
*/class Person {
	
	private String name;
	private int age;
	
	public Person() {
	}
	public Person(String name) {
		this.name = name;	//this关键字第一种用法
	}
	public Person(String name,int age) {
		this(name);	 //this 关键字第二种用法,这里是不能写成 Person(name) 的，因为构造方法是系统调用的
		this.age = age;
	}
	
	public void fun1() {
	
		System.out.println(name + "'s age is " + age);	
	}
	
	public void fun2() {
		
		Person p2 = new Person("lisi",10);
		p2.fun1();
		//在这里如何得到调用 fun2方法 的那个对象呢？这里就可以用 this 关键字
		this.fun1(); //this 关键字的第三中用法,这里 this 关键字不是必需的,也可以直接写成 fun1();
	}
	
	public static void main(String[] args) {
			
			Person p1 = new Person("zhangsan",20);
			p1.fun2();
	}
}
```

```
运行结果：
```

```
lisi's age is 10
zhangsan's age is 20
```