cloneable

Java's “clone()” method generator for Eclipse Galileo

╄→гoц情女王★ 提交于 2019-12-23 07:57:03
问题 What is the best tool for java's clone() method generation in Eclipse Galileo available from repositories? What is the reason, that prevents Eclipse developers from including this tool in standard release? 回答1: It's very hard to implement clone() right. It is considered not a good practice to do so. Bloch (Effective Java) suggest that using clone() should be avoided. Use other means of shallow cloning, like copy-constructors or utilities like commons-beanutils. 回答2: I absolutely agree with

Does cloning provide a performance improvement over constructors/factory methods?

南楼画角 提交于 2019-12-22 05:05:14
问题 I'm maintaing an older Java code base (jvm 1.4) that seems to use cloning as an alternative to object instantiation, I'm guessing as a performance optimization. Here's a contrived example: public class Foo { private SomeObject obj; // SomeObject implements Cloneable public Foo() { obj = new SomeObject(); obj.setField1("abc"); // these fields will have the same value every time obj.setField2("def"); } public void doStuff() { SomeObject newObj = obj.clone(); // clone it instead of using a

Why Object clone() method available only to classes that implement Cloneable interface? [duplicate]

℡╲_俬逩灬. 提交于 2019-12-20 10:04:14
问题 This question already has answers here : Why is the clone() method protected in java.lang.Object? (11 answers) Closed 6 years ago . I know that clone() is a protected method, but "protected" means that it is accessible for all subclasses of particular class. Any Java class is a subclass of Object , so what is the reason for the protected method here? And why can we call clone() only on classes that implement the Cloneable interface? I can't understand how it connects to the fact that clone()

Invalid covariant type with CRTP clonable class

只愿长相守 提交于 2019-12-18 08:29:24
问题 I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I made this code below, and the compiler shout at me this error: main.cpp:12:5: error: return type of virtual function 'clone' is not covariant with the return type of the function it overrides ('B *' is not derived from 'AbstractClonable *') The class

Invalid covariant type with CRTP clonable class

泄露秘密 提交于 2019-12-18 08:28:18
问题 I'm trying to implement a Clonable class with the CRTP. However, I need to have abstract class that have a pure virtual clone method, overridden by child classes. To make this happen, I need the clone function to return a covariant return type. I made this code below, and the compiler shout at me this error: main.cpp:12:5: error: return type of virtual function 'clone' is not covariant with the return type of the function it overrides ('B *' is not derived from 'AbstractClonable *') The class

Prototype Pattern in Java - the clone() method

一世执手 提交于 2019-12-18 04:42:15
问题 So, I've been reading on Design Patterns and the Prototype Patterns confuses me. I believe one of the points of using it is avoiding the need for using the new operator. Then I look at this example: http://sourcemaking.com/design_patterns/prototype/java/1 First, Their idea of Prototype implements a clone() method, which is weird. Wikipedia also says I need a pure virtual method clone to be implemented by subclasses (why?). Doesn't Java already provide such a method, doing exactly what we need

How to clone a struct storing a boxed trait object?

南楼画角 提交于 2019-12-17 03:16:31
问题 I wrote a program that has the trait Animal and the struct Dog implementing the trait. It also has a struct AnimalHouse storing an animal as a trait object Box<Animal> . trait Animal { fn speak(&self); } struct Dog { name: String, } impl Dog { fn new(name: &str) -> Dog { return Dog { name: name.to_string(), }; } } impl Animal for Dog { fn speak(&self) { println!{"{}: ruff, ruff!", self.name}; } } struct AnimalHouse { animal: Box<Animal>, } fn main() { let house = AnimalHouse { animal: Box:

Why should I implement ICloneable in c#?

为君一笑 提交于 2019-12-17 02:11:56
问题 Can you explain to me why I should inherit from ICloneable and implement the Clone() method? If I want to do a deep copy, can't I just implement my method? Let's say MyClone() ? Why should I inherit from ICloneable ? What are the advantages? Is it just a matter of making code "more readable"? 回答1: You shouldn't. Microsoft recommends against implementing ICloneable because there's no clear indication from the interface whether your Clone method performs a "deep" or "shallow" clone. See this

instanceof - incompatible conditional operand types

丶灬走出姿态 提交于 2019-12-17 01:01:33
问题 The following compiles fine: Object o = new Object(); System.out.println(o instanceof Cloneable); But this doesn't: String s = new String(); System.out.println(s instanceof Cloneable); A compiler error is thrown. What is the problem? 回答1: A more blatant incarnation of your problem is the following: if ("foo" instanceof Number) // "Incompatible conditional operand types String and Number" This is specified in JLS 15.20.2 Type comparison operator instanceof: RelationalExpression:

How to properly override clone method?

被刻印的时光 ゝ 提交于 2019-12-16 19:57:48
问题 I need to implement a deep clone in one of my objects which has no superclass. What is the best way to handle the checked CloneNotSupportedException thrown by the superclass (which is Object )? A coworker advised me to handle it the following way: @Override public MyObject clone() { MyObject foo; try { foo = (MyObject) super.clone(); } catch (CloneNotSupportedException e) { throw new Error(); } // Deep clone member fields here return foo; } This seems like a good solution to me, but I wanted