我是Java新手。在此代码片段中,为什么 - if(t.canRecord == true) - 当为Object时,只存储引用并且不覆盖现有值。
class TapeDeck
{
boolean canRecord = false;
void playTape()
{
System.out.println("tape playing");
}
void recordTape()
{
System.out.println("tape recording");
}
}
class TapeDeckTestDrive
{
public static void main(String [] args)
{
TapeDeck t=new TapeDeck();
t.canRecord = true;
t.playTape();
if (t.canRecord == true)
{
t.recordTape();
}
}
}
TapeDeck
的新实例。对于此实例,canRecord
is的值设置为false
。canRecord
未设置为私人可见性,因此您可以通过t.canRecord
直接访问它。通过t.canRecord = true
你不是压倒性的,而是用false
覆盖true
的现有价值。true
as(i)t.canRecord
是true
和(ii)与true
中的true
results的比较。