【问题标题】:How can this implementation of string constructor work (java)?字符串构造函数的这种实现如何工作(java)?
【发布时间】:2012-05-09 15:44:19
【问题描述】:
 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 字段是私有的,但是这段代码似乎以某种方式直接达到了这些值。这怎么可能?

【问题讨论】:

    标签: java string constructor private field


    【解决方案1】:

    private 表示“只能被类访问”,而不是“只能被对象访问”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 2019-01-21
      • 2015-10-19
      • 2021-06-18
      • 1970-01-01
      相关资源
      最近更新 更多