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

📄 cloneexam.java

📁 Java程序设计实用教程源代码 本书源代码按章分别放置在不同的文件夹中,所有程序均在JDK1.6环境下编译运行正常,除了第13章需要建立ODBC数据源之外,其他程序只要有Java运行环境即可直接运行
💻 JAVA
字号:
package clone;
class Pen
    implements Cloneable {
  String color;
  int length;
  Pen(String color, int length) {
    this.color = color;
    this.length = length;
  }

  void setColor(String color) {
    this.color = color;
  }

  String getColor() {
    return color;
  }

  void setLength(int length) {
    this.length = length;
  }

  int getLength() {
    return length;
  }

  void test() {
    myPen mp = new myPen();
    mp.display();
  }

  class myPen {
    void display() {
      System.out.println("the length of My " + color + "Pen is " + length);
    }
  }

  protected Object clone() {
    try {
      Pen clonedObject = (Pen)super.clone();
      return clonedObject;
    }
    catch (CloneNotSupportedException e) {
      throw new InternalError();
    }
  }
}

public class CloneExam {
  public static void main(String args[]) {
    int objcount = 0;
    Pen p[] = new Pen[10];
    Pen a = new Pen("yellow", 13);
    for (int i = 0; i < 10; i++) {
      p[i] = (Pen) a.clone();
      p[i].test();
      objcount = objcount + 1;
    }
    System.out.println("the acounts of clone object:" + objcount);
  }
}

⌨️ 快捷键说明

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