b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

解释器模式举例_lua解释器交互模式_动态编译与解释器模式(11)

电脑杂谈  发布时间:2017-02-22 17:29:35  来源:网络整理

简单的原型模式就是在clone()实现时,new一个新的实例,然后为成员变量赋后返回。

Java的原生支持

Java中所有类都直接或间接继承自Object类,Object类中已有clone()方法:”protected native Object clone() throws CloneNotSupportedException;“,可以看到权限是protected的,所以仅有子类可以访问这个方法,但我们可以在子类中重写这个方法,将访问权限上调到public,然后方法体里面return super.clone()。

我们能看到这个Object方法是可能会抛出异常的,我们必须实现Cloneable接口,才可以使用这个方法,否则会抛出“java.lang.CloneNotSupportedException”的异常。这个Cloneable接口其实是空的,实现它的目的在于让JVM知道这个对象是可以可复制的,否则clone()时就会发生异常。下面看演示代码:

//使用 java 自带的支持public class APITestUse {    public static void main(String args[]) throws CloneNotSupportedException{        MyObject myObject = new MyObject();        myObject.i = 500;        MyObject myObjectClone = (MyObject) myObject.clone();        System.out.println(myObjectClone.i);    }}//一个可以复制的对象class MyObject implements Cloneable{    int i;    public Object clone() throws CloneNotSupportedException{        return super.clone();    }}//结果会输出 500

调用这个方法时,成员变量会自动被复制。所以如果需要使用原型模式,Java原生支持就已经很好用了。

除了以上的原生支持,java中还有一种序列化,只需要对象实现Serializable接口。这样,我们可以将对象写入到流中,可以保存到文件,也可以通过网络发送到其他地方:

//使用Serializable支持克隆public class SerializablePrototype implements Serializable{    private static final long serialVersionUID = 1L;    private int i;    private transient int notClone;//transient关键字的成员不会被序列化    public int getI() {        return i;    }    public void setI(int i) {        this.i = i;    }    public int getNotClone() {        return notClone;    }    public void setNotClone(int notClone) {        this.notClone = notClone;    }    public void writeToFile(String path) throws Exception{        FileOutputStream outStream = new FileOutputStream(path);        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outStream);          objectOutputStream.writeObject(this);          outStream.close();    }    public SerializablePrototype ReadFromFile(String path) throws Exception{        File file = new File(path);        if(!file.exists())            file.createNewFile();        FileInputStream inStream = new FileInputStream(path);        ObjectInputStream objectOutputStream = new ObjectInputStream(inStream);          Object o= objectOutputStream.readObject();          inStream.close();        return (SerializablePrototype) o;    }    public static void main(String args[]) throws Exception{        String path = "D:/SerializablePrototype.instance";        SerializablePrototype prototype = new SerializablePrototype();        prototype.setI(123);        prototype.setNotClone(456);        prototype.writeToFile(path);        SerializablePrototype prototypeClone = new SerializablePrototype();        prototypeClone = prototype.ReadFromFile(path);        System.out.println(prototypeClone.getI()  " "  prototypeClone.getNotClone());    }}//输出:123 0


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-33893-11.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    • 刘舟
      刘舟

      另外两个送给了我最重要的两个人

    热点图片
    拼命载入中...