weixin_38096595 2017-04-19 05:00 采纳率: 0%
浏览 1391

关于String源代码中equal方法的Count属性是如何获取到值的???

public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
这里面的Count值是如何获取的?
/** The count is the number of characters in the String. */
private final int count;

这里声明的是final类型的成员变量 默认值是0 那么这里的equal中Count值肯定不是0 各位大神赐教

  • 写回答

1条回答 默认 最新

  • yizishou 2017-04-19 07:30
    关注

    final类型的成员变量可以在对象的初始化代码中赋值一次。

    以下代码均是可以编译通过的:

    private final int count = 10;
    
     private final int count;
    
     public 构造函数() {
         count = 10;
     }
    
     private final int count;
    
    {
         count = 10;
     }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制