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();
}
}
(创建型模式五)原型模式
最新推荐文章于 2024-08-29 11:06:44 发布