⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcloneable.java

📁 此源码为机械工业出版社出版的《Java语言程序设计》第三版所配套的书中所有源代码。
💻 JAVA
字号:
// TestCloneable.java: Use the TestCloneable interface
// to enable cloning
public class TestCloneable
{
  // Main method
  public static void main(String[] args)
  {
    // Declare and create an instance of CloneableCircle
    CloneableCircle c1 = new CloneableCircle(5);
    CloneableCircle c2 = (CloneableCircle)c1.clone();

    System.out.println("After copying c1 to circl2");

    // Check if two variables point to the same object
    if (c1 == c2)
      System.out.println("c1 and c2 reference to the same object");
    else
      System.out.println("c1 and c2 don't point to the same object");

    // Check if two objects are of identical contents
    if (c1.equals(c2))
      System.out.println("c1 and c2 have the same contents");
    else
      System.out.println("c1 and c2 don't have the same contents");

    // Modify c1's radius, name
    c1.setRadius(10);
    c1.getCreator().setFirstname("Michael");
    c1.getCreator().setMi("Z");

    // Display c1 and c2
    System.out.println("\nAfter modifying c1");
    System.out.println("c1 " + c1);
    System.out.println("c2 " + c2);

    System.out.println();
    if (c1 instanceof Cloneable)
    {
      System.out.println("A CloneableCircle objec is cloneable");
    }
    else
    {
      System.out.println("A CloneableCircle objec is not cloneable");
    }

    // Check if a Circle object is cloneable
    Circle c = new Circle();
    if (c instanceof Cloneable)
    {
      System.out.println("A Circle object is cloneable");
    }
    else
    {
      System.out.println("A Circle object is not cloneable");
    }
  }
}

// CloneableCircle is a subclass of Circle, which implements the
// Cloneable interface
class CloneableCircle extends Circle implements Cloneable
{
  // Store the creator of the object
  private Name creator = new Name("Yong", "D", "Liang");
  
  // Construct a CloneableCircle with specified radius
  public CloneableCircle(double radius)
  {
    super(radius);
  }

  // Getter method for creator
  public Name getCreator()
  {
    return creator;
  }

  // Setter method for creator
  public void setCreator(Name name)
  {
    creator = name;
  }

  // Override the protected clone method defined in the Object class
  public Object clone()
  {
    try
    {
      return super.clone();
    }
    catch (CloneNotSupportedException ex)
    {
      return null;
    }
  }

  // Override the toString method defined in the Object class
  public String toString()
  {
    return super.toString() + " " + creator.getFullname();
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -