码迷,mamicode.com
首页 > 其他好文 > 详细

(创建型模式五)原型模式

时间:2015-04-24 22:45:07      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:原型模式

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!