java关键字this使用

1.局部变量与成员变量重名时,成员变量被覆盖,要使用则需用this


2.访问本类的另外一条构造方法

   用法:   this(实参表);      //必须放在构造函数第一条语句

package com.xiyou.weijie.chap5;

public class TestThis {
	private int a;
	private int b;
	private String s;
	
	public TestThis()
	{
		System.out.println("无参构造");
	}
	
	public TestThis(int a,int b)
	{
		this.a=a;
		this.b=b;
		System.out.println("有参构造1");
	}
	public TestThis(int a,int b,String s)
	{
		this(a,b);  //访问本类<span style="font-family:Calibri;">的有参构造1</span>
		this.a=a;
		this.b=b;
		this.s=s;
		System.out.println("有参构造2");
	}
	
	public static void main(String[]args)
	{
		TestThis t=new TestThis(14,37,"Janie");
		
	}
}

发布了31 篇原创文章 · 获赞 8 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weijie_home/article/details/49450905
今日推荐