⑴ Shallow copy (shallow clone)
All the variables of the copied object contain the same values as the original object, and all references to other objects still point to the original object. In other words, a shallow copy replicates only the object
Builder modeldefinitionAlso called the generator pattern, it can abstract the construction process of complex objects (abstract categories) so that different implementations of this abstract process can construct objects of different performance
The (Protected) clone () method is defined in the Java object class, and if its own class requires a Clone method, the Cloneable interface is implemented, the Clone () method is overridden, and the method access level is changed to (public). However,
Prototype Mode prototypePrototype mode is also a pattern of object creation, and the main purpose is to create new objects by copying existing objects. It has the advantage of not having to use the new one every time you instantiate the object,
Comparison of 1.clone () and "="
An assignment in a primitive type variable such as int a = 1;int B = A;a has the same value as B, changing the value of a or B does not affect each other.
But between objects:
Java.util.Date date1 = new
Always assign a value equal to the number, today found some inexplicable mistakes, read a lot of articles just found that the original object assignment can not be equal to the number, equal to only the main type of assignment (String,int,float, and
Prototype mode is also a very simple pattern, for Java already has a corresponding interface (cloneable). About the prototype mode > is to put the resume as an example to explain, that I want to put a lot of resumes, in fact, each resume is the
Java Object Copy FAQ@author Ixenosabout clone[object Copy]
In the actual programming process, sometimes we will encounter a situation: When you have an object A, at some point, a has saved the corresponding property values, and the values
1. Explicitly implement Interface membersIn order to implement an interface, a class can define an explicit interface member execution body (EXPLICIT interface member implementations). An explicit interface member execution body can be a method, a
Why cloning is requiredIn the actual programming process, we often encounter this situation: there is an object A, in a moment a is already included in a number of valid values, it may be necessary to a exactly the same new object B, and then any
Duplicate of reference type in heap and use the clone interface ICloneable to fix itNavigationIn-depth illustration C # Heap and Stack C # Heap (ing) VS Stack (ing) Section 1 Understanding Heap and Stack depth diagram C # Heap and Stack C # Heap
Public ATM database deepclone () {memorystream MS = new memorystream (); binaryformatter BF = new binaryformatter (); BF. serialize (MS, this); Ms. position = 0; return BF. deserialize (MS) as ATM database ;}
C # supports two types: "value type"
I like this mode very much because it solves the problem of object assignment. In particular, the clone method is fresh in your memory.
Use Case: The design mode is used when a system is created, constructed, and expressed independently of the
What
Prototype: Use a prototype instance to specify the type of the object to be created, and copy the prototype to create a new object.
Why
Prototype is applicable when a class instance has several different combinations of States, it is easier
1. Collection
1.1 Method
(1) iterator iterator (); returns an iterator used to iterate elements in the set.
(2) int size (); returns the number of elements in the set.
(3) Boolean isempty (); determines whether the current set is null. If it is
Shallow copy:
Package clonemethod; public class shallowcopy {public static void main (string [] ARGs) throws clonenotsupportedexception {student s = new student ("S", 12 ); teacher T = new teacher ("T", 40, S); teacher Ct = (teacher) T. clone ();
Section 5 interface implementation
1. explicitly implement interface members
To implement the interface, the class can define the explicit interface member execution bodies (explicit interface member implementations ). The explicit interface
Transferred from: http://www.cnblogs.com/focusChen/articles/2497768.htmlKeyword: Java object referenceJava objects and their referencesSome basic concepts between objects and references.Beginner Java, for a long time, always feel that the basic
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.