cloneablebike.java

来自「《JAVA与模式》附书中源代码」· Java 代码 · 共 35 行

JAVA
35
字号
package com.javapatterns.prototype.bike;

public class CloneableBike implements Cloneable   // Class we will be cloning
{
   private int gears, wheelSize, age;      // Local variables

   public CloneableBike (int gears, int wheelSize)  // Class constructor
   {
      this.gears = gears;
      this.wheelSize = wheelSize;
      age = 0;
   }

   public void setAge(int age) { this.age = age; }  // Set the bike age
   public int getAge()       { return age; }
   public int getGears()     { return gears; }
   public int getWheelSize() { return wheelSize; }

   // Method to allow cloning of this class

   public Object clone()
   {
      // Create a new CloneableBike object ready for returning to the caller.

      CloneableBike temp = new CloneableBike(gears, wheelSize);
      temp.setAge(age);

      // Return this new CloneableBike object to the caller, as an Object
      // class object

      return (Object) temp;
   }
}

⌨️ 快捷键说明

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