cloneable

Effective Java: Analysis of the clone() method

北战南征 提交于 2019-11-27 01:50:59
问题 Consider the following from Effective Java Item 11 (Override clone judiciously) where Josh Bloch is explaining what is wrong with the clone() contract . There are a number of problems with this contract. The provision that “no constructors are called” is too strong. A well-behaved clone method can call constructors to create objects internal to the clone under construction. If the class is final, clone can even return an object created by a constructor. Can someone explain what Josh Bloch is

Why is Cloneable not deprecated?

自闭症网瘾萝莉.ら 提交于 2019-11-26 23:54:52
问题 It is commonly understood that Cloneable interface in Java is broken. There are many reasons for this, which I will not mention; others already did it. It is also the position of Java architects themselves. My question is therefore: why has is not been deprecated yet? If the core Java team have decided that it is broken, then they must also have considered deprecation. What are their reasons against doing so (in Java 8 it is still not deprecated)? 回答1: There is a bug submitted in 1997 to Java

How to clone a struct storing a boxed trait object?

孤街醉人 提交于 2019-11-26 17:50:19
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::new(Dog::new("Bobby")), }; house.animal.speak(); } It returns "Bobby: ruff, ruff!" as expected, but if I

Why should I implement ICloneable in c#?

狂风中的少年 提交于 2019-11-26 11:15:54
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"? Matt Hamilton 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 blog post from Brad Abrams back in 2003(!) for more information. supercat The ICloneable

instanceof - incompatible conditional operand types

爷,独闯天下 提交于 2019-11-26 10:30:44
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? 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: RelationalExpression instanceof ReferenceType If a cast of the RelationalExpression to the ReferenceType would be

Java: Rationale of the Cloneable interface

女生的网名这么多〃 提交于 2019-11-26 08:26:57
问题 Why wasn\'t the .clone() method specified in the java.lang.Cloneable interface ? 回答1: Basically, it's a broken interface. Ken Arnold and Bill Venners discussed it in Java Design Issues. Arnold: If I were to be God at this point, and many people are probably glad I am not, I would say deprecate Cloneable and have a Copyable , because Cloneable has problems. Besides the fact that it's misspelled, Cloneable doesn't contain the clone method. That means you can't test if something is an instance

About Java cloneable

拥有回忆 提交于 2019-11-26 00:37:21
问题 I was looking for some tutorials explaining about Java Cloneable , but did not get any good links, and Stack Overflow is becoming more obvious choice anyways. I would like to know the following: Cloneable means we can have a clone or a copy of objects, by implementing the Cloneable interface. What are the advantages and disadvantages of doing that? How does the recursive cloning happen if the object is a composite object? 回答1: The first thing you should know about Cloneable is - don't use it.