gpt4 book ai didi

java - java中String类如何用构造函数初始化

转载 作者:行者123 更新时间:2023-12-02 02:09:03 24 4
gpt4 key购买 nike

  public String(String original) {
this.value = original.value;
this.hash = original.hash;
}

我不明白如何将原始数据转换为字符数组。如果我用不同的代码尝试它,它会抛出编译错误。

最佳答案

String 有两个属性:

/** The value is used for character storage. */
private final char value[];

/** Cache the hash code for the string */
private int hash; // Default to 0

因为您位于 String 的构造函数内,所以您有权访问其私有(private)字段 value[]hash

您无法从 String 类外部访问这些 private 字段,因此如果您尝试这样做,将会抛出编译错误。

关于java - java中String类如何用构造函数初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50302710/

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