克隆与实例化新类

发布于 2024-09-18 16:06:52 字数 520 浏览 1 评论 0原文

在这种情况下,克隆是好的做法吗?怎样才能做得更好呢?

public ModelCollection startParsing() {

   return parseFeed(new ModelSpecialEntry); 
}

public ModelCollection parseFeed(ModelEntry pattern)  {

   ModelCollection modelCollection = new ModelCollection();

   while( condition ) {

     //TODO: Is cloning the best solution?
     ModelEntry model = (ModelEntry) pattern.clone();



     model.parse();

     //add this item to an collection
     modelCollection.add(model);


   }

   return modelCollection;
}

Is cloning good practice in this case? How to do it better?

public ModelCollection startParsing() {

   return parseFeed(new ModelSpecialEntry); 
}

public ModelCollection parseFeed(ModelEntry pattern)  {

   ModelCollection modelCollection = new ModelCollection();

   while( condition ) {

     //TODO: Is cloning the best solution?
     ModelEntry model = (ModelEntry) pattern.clone();



     model.parse();

     //add this item to an collection
     modelCollection.add(model);


   }

   return modelCollection;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不…忘初心 2024-09-25 16:06:53

在 Java 中,克隆很少是一个好主意。尝试其他技术,例如复制构造函数或工厂方法。

维基百科有一篇不错的文章介绍了为什么clone() Java有很多缺点。

使用复制构造函数,创建一个将当前类的实例作为参数的构造函数,并复制本地类中的所有字段:

public class Foo {

    private String bar;
    private String baz;

    public Foo(Foo other) {
        this.bar = other.bar;
        this.baz = other.baz;
    }

}

使用工厂方法,创建一个将对象作为参数的方法,并返回一个包含相同值的对象:

public Foo copyFoo(Foo other) {
    Foo foo = new Foo();
    foo.setBar(other.getBar());
    foo.setBaz(other.getBaz());
}

Cloning is rarely a good idea in Java. Try other techniques such as Copy constructors or Factory methods.

Wikipedia has a nice article on why clone() has many disadvantages in Java.

Using copy constructors, create a constructor that takes an instance of the current class as parameter, and copy all fields in the local class:

public class Foo {

    private String bar;
    private String baz;

    public Foo(Foo other) {
        this.bar = other.bar;
        this.baz = other.baz;
    }

}

Using factory methods, create a method that takes your object as parameter and return an object containing the same values:

public Foo copyFoo(Foo other) {
    Foo foo = new Foo();
    foo.setBar(other.getBar());
    foo.setBaz(other.getBaz());
}
旧人 2024-09-25 16:06:53

您可以使用复制构造函数而不是实现 可克隆,但看起来您有一个 ModelEntry 类的层次结构,因此使用clone 可能是最好的方法。请参阅此问题,了解有关 Cloneable

You could use a copy constructor instead of implementing Cloneable, but it looks like you have a hierarchy of ModelEntry classes, so using clone may be the best approach. See this question for some pointers on whats wrong with Cloneable

无名指的心愿 2024-09-25 16:06:53

我认为,这就像编程中的其他一切一样:它取决于对象规范。

尝试做一个非常快速的测试:
克隆 100000 个对象并实例化相同数量的对象并检查需要多长时间 (System.currentTimeInMilis())。通常克隆速度更快。

请记住,克隆有一个问题 - 添加新字段等时,您也需要修改 clone() 方法。

I thing, that it is like with everything else in programming: it depends on the object specyfication.

Try to make a really quick test:
clone 100000 objects and instantiates the same amount of objects and check time how long it takes (System.currentTimeInMilis()). Often clone is faster.

And remember that with clone there is one problem - when adding new field etc. you need to modify clone() method too.

闻呓 2024-09-25 16:06:53

许多程序员都同意,克隆并不是一个好主意。

这是很容易出错的。您必须小心地重写 clone()。忘记调用 super.clone() 是一个常见的错误。

Clone is not a good idea as many programmers agree.

It's error-prone. You have to override clone() carefully.Forgetting invoking super.clone() is a popular bug.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文