【Java开源代码栏目提醒】:网学会员在Java开源代码频道为大家收集整理了CloneableBike.java提供大家参考,希望对大家有所帮助!
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;
}
}