package com.eyugame.modle; /*原型类*/ public abstract class Prototype implements Cloneable { private int x; private int y; /*复制*/ public Prototype clone() { Prototype prototype = null; try { prototype = (Prototype) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } prototype.x = this.x; prototype.y = this.y; return prototype; } public int getX() { return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; } abstract void show(); } /*子类*/ class MyPrototype extends Prototype{ @Override public void show() { System.out.println("原型模式"); } } class TestPrototype{ public static void main(String[] args) { MyPrototype mp=new MyPrototype(); MyPrototype mp2=(MyPrototype) mp.clone(); mp2.show(); } }
原文:http://blog.csdn.net/h348592532/article/details/45252231