2012-04-29 78 views
3
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; 
} 

對不起,如果我是愚蠢的,但我不明白。計數和值字段是私有的,但這些代碼似乎以某種方式直接達到這些值。怎麼會這樣?字符串構造函數的這個實現如何工作(java)?

回答

5

private表示「只能由類訪問」,而不是「僅可由對象訪問」。

相關問題