cloneable

How to implement ICloneable without inviting future object-slicing

孤街浪徒 提交于 2020-08-08 05:31:06
问题 My question is about how to implement the classic ICloneable interface in such a way that it won't lead to inadvertent object-slicing when a future programmer isn't paying close attention. Here's an example of the kind of programming error I'd like to detect (preferably at compile-time): #include <stdio.h> class ICloneable { public: virtual ICloneable * clone() const = 0; }; class A : public ICloneable { public: A() {} A(const A & rhs) {} virtual ICloneable * clone() const {return new A(*this

No clone method in String Class

*爱你&永不变心* 提交于 2020-07-15 04:05:19
问题 A technical aptitude question HashMap<String, String> map = new HashMap<String,String>(); String key1 = "key1"; map.put(key1, "value1"); String key2 = key1.clone(); map.put(key2, "value2"); What are the contents of the map object? I answered it as {key1=value2} but later realized that String doesn't contain clone method. I wanted to know the reason for the same. 回答1: String is an immutable object, so it needn't a clone method since the client code can't change its state inside the String

Why does java.lang.Cloneable not override the clone() method in java.lang.Object?

℡╲_俬逩灬. 提交于 2020-05-12 16:57:59
问题 The Java specification for the java.lang.Cloneable interface defines itself as signifying that any object that extends it also has implemented the clone() method that rests dormant within java.lang.Object . Specifically, it says that: A class implements the Cloneable interface to indicate to the java.lang.Object#clone() method that it is legal for that method to make a field-for-field copy of instances of that class. To me, this means that it should be assumed that every class that extends

Why does java.lang.Cloneable not override the clone() method in java.lang.Object?

本秂侑毒 提交于 2020-05-12 16:57:09
问题 The Java specification for the java.lang.Cloneable interface defines itself as signifying that any object that extends it also has implemented the clone() method that rests dormant within java.lang.Object . Specifically, it says that: A class implements the Cloneable interface to indicate to the java.lang.Object#clone() method that it is legal for that method to make a field-for-field copy of instances of that class. To me, this means that it should be assumed that every class that extends

Why doesn't CopyOnWriteArraySet implement the Cloneable interface, while CopyOnWriteArrayList does?

寵の児 提交于 2020-01-01 07:11:08
问题 In this bug report, Doug Lea writes (referring to a pre-release version of JDK 5.0): While CopyOnWriteArraySet is declared Cloneable , it fails to define public clone method. But it eventually ends up that CopyOnWriteArraySet doesn't implement the Cloneable interface at all! (This is true in Java SE 6, 7, and 8.) How is CopyOnWriteArraySet different from CopyOnWriteArrayList with respect to cloning? There is a sound reason nobody ever wants to clone it? P.S. I understand that clone() is not

Implementing a Deck of Cards in Java

跟風遠走 提交于 2019-12-25 12:25:14
问题 So I have a lab (we are allowed to seek outside help on it, so here I am after lots of head scratching) where we have to implement a deck of cards. We have to use the enum class to create num For Suits: public enum Suits { CLUBS, HEARTS, DIAMONDS, SPADES } For Numerals: public enum Numerals { DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13), ACE(14); } My card class is pretty straightforward, but I'm not sure about these two

Pretty HABTM List Entry

非 Y 不嫁゛ 提交于 2019-12-25 03:50:23
问题 I have a Recipe, Item, and Units table/model. I have a HABTM relationship with Recipe and Item, and I get the default multiple-select box when adding/editing Recipe. (am using Bake for everything for the most part). The problem is I need to associate quantities and units with each Item. Sample of UI I'm hoping for: A big component of it is the ability to add/delete/edit the individual items. I imagine looking at the submitted form data, and using some jquery and clone would work. But I was

How to use Cloneable type as parameter to Java generic class

社会主义新天地 提交于 2019-12-23 17:58:41
问题 I have a generic class that needs to be able to clone objects of the parameter type. A very simple example is below. The compiler claims clone() from the type Object is not visible. public class GenericTest<T extends Cloneable> { T obj; GenericTest(T t) { obj = t; } T getClone() { // "The method clone() from the type Object is not visible." return (T) obj.clone(); } } I'd prefer not to have the caller do the cloning since there are other things that have to happen to maintain the integrity of

Why shouldn't an object be cloneable? [closed]

☆樱花仙子☆ 提交于 2019-12-23 16:41:06
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . I read lots of threads about the clone() method of Object and the Cloneable Interface but I couldn't find a legitimate answer to my

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

你离开我真会死。 提交于 2019-12-23 07:57:15
问题 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