gpt4 book ai didi

java - Java中的引用变量?

转载 作者:行者123 更新时间:2023-12-02 05:17:58 25 4
gpt4 key购买 nike

我的疑问是引用 Rose 对象的引用变量“r”现在引用 Flower 对象。

玫瑰对象现在怎么样了?会被毁掉吗?

我有以下代码:

class Flower
{
public void smell() // I
{
System.out.println("All flowers give smell, if you can smell");
}
}
public class Rose extends Flower
{
public void smell() // II
{
System.out.println("Rose gives rosy smell");
}
public static void main(String args[])
{
Flower f = new Flower();
Rose r = new Rose();

f = r; // subclass to super class, it is valid
f.smell(); // II

}
}

最佳答案

enter image description here

它是符合垃圾回收条件的Flower对象。Rose 对象仍然由引用变量 fr 引用。

关于java - Java中的引用变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16668384/

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