码迷,mamicode.com
首页 > 其他好文 > 详细

this关键字

时间:2016-11-27 00:29:12      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:this   关键字   

this关键字的作用:

  1. 调用类中的属性

  2. 调用类中的方法或构造方法

  3. 表示当前对象


public class Test {

	public static void main(String[] args) {
		
	Person p1=new Person();

	}

}


class Person{
	private String name;
	private char sex;
	
	public Person(){
		this("张爷爷");
	}
	
	public Person(String name){
		this(name,‘男‘);
	}
	
	public Person(String name,char sex){
		this.name=name; //表示当前对象属性
		this.sex=sex; //表示当前对象属性
		this.fight(); //调用本类中的方法
	}
	
	public void fight(){
		System.out.println("呔!"+this.name+"在此,谁敢跟俺一战!");
	}
	
}


this关键字

标签:this   关键字   

原文地址:http://12170190.blog.51cto.com/12160190/1876873

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!