📜  Java |功能|问题10

📅  最后修改于: 2021-06-29 20:45:59             🧑  作者: Mango

预测以下程序的输出。

class Test implements Cloneable
{
    int a;
  
    Test cloning()
    {
        try
        {
            return (Test) super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            System.out.println("CloneNotSupportedException is caught");
            return this;
        }
    }
}
  
class demo
{
  
    public static void main(String args[])
    {
        Test obj1 = new Test();
        Test obj2;
        obj1.a = 10;
        obj2 = obj1.cloning();
        obj2.a = 20;
  
        System.out.println("obj1.a = " + obj1.a);
        System.out.println("obj2.a = " + obj2.a);
    }
}

(一种)

obj1.a = 10
obj2.a = 20

(B)

obj1.a = 20
obj2.a = 20

(C)

obj1.a = 10
obj2.a = 10

答案: (A)
说明: clone()方法生成在其上被调用的对象的副本。只能克隆实现Cloneable接口的类。
这个问题的测验
如果您在以上帖子中发现任何错误,请在下面发表评论