gpt4 book ai didi

java - 这个字符串构造函数的实现如何工作(java)?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:53:51 26 4
gpt4 key购买 nike

 public String(String original) {
int size = original.count;
char[] originalValue = original.value;
char[] v;
if (originalValue.length > size) {
// The array representing the String is bigger than the new
// String itself. Perhaps this constructor is being called
// in order to trim the baggage, so make a copy of the array.
int off = original.offset;
v = Arrays.copyOfRange(originalValue, off, off+size);
} else {
// The array representing the String is the same
// size as the String, so no point in making a copy.
v = originalValue;
}
this.offset = 0;
this.count = size;
this.value = v;
}

对不起,如果我是愚蠢的,但我不明白。 count 和 value 字段是私有(private)的,但这段代码似乎以某种方式直接达到了这些值。怎么会这样?

最佳答案

private 的意思是“只能被类访问”,而不是“只能被对象访问”。

关于java - 这个字符串构造函数的实现如何工作(java)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10375265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com