String的value被定义成私有属性,为什么构造方法中还能使用 this.value = "".value?

宵月xiaoyue 2020-03-17 08:45:08
private final char value[];
String源码中value被定义成私有属性,为什么这个构造方法还可以使用 "".value 呢?


public String() {
this.value = "".value;
}
...全文
150 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qybao 2020-03-17
  • 打赏
  • 举报
回复
private属性只在本类内可见,注意是本类,不是本对象。“”是String对象,也属于String本类。
Little5 2020-03-17
  • 打赏
  • 举报
回复
public class Test
{
	private int i;
	Test        obj;

	Test(Test t)
	{
		this.obj = t;
		System.out.println(this.i);// 0
		System.out.println(obj.i);// 9
		this.i = obj.i;
	}

	Test(int i)
	{
		this.i = i;
	}

	public int getI()
	{
		return i;
	}

	public void setI(int i)
	{
		this.i = i;
	}

	public static void main(String[] args)
	{
		Test a = new Test(9);
		Test b = new Test(a);
		System.out.println(b.getI());// 9
	}
}
我的理解是本类的对象在本类里可以访问私有属性,虽然这个对象和当前的this不同。

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧