- 浏览: 31403 次
- 性别:
- 来自: 杭州
最近访客 更多访客>>
最新评论
-
bbym010:
通俗易懂,不错
Build模式的理解 -
0000ps:
今天在MSDN上看到一段内容:
Value Object. S ...
Value Object vs. Data Transfer Object (VO vs. DTO)
Copy constructors :
Example
Example run of this class :
>java -cp . Galaxy
M101 mass: 15.0
M101Copy mass: 25.0
M101 mass: 15.0
M101CopyTwo mass: 35.0
- provide an attractive alternative to the rather pathological clone method
- are easily implemented
- simply extract the argument's data, and forward to a regular constructor
- are unnecessary for immutable objects
Example
public final class Galaxy { public Galaxy (double aMass, String aName) { fMass = aMass; fName = aName; } /** * Copy constructor. */ public Galaxy(Galaxy aGalaxy) { this(aGalaxy.getMass(), aGalaxy.getName()); //no defensive copies are created here, since //there are no mutable object fields (String is immutable) } /** * Alternative style for a copy constructor, using a static newInstance * method. */ public static Galaxy newInstance(Galaxy aGalaxy) { return new Galaxy(aGalaxy.getMass(), aGalaxy.getName()); } public double getMass() { return fMass; } /** * This is the only method which changes the state of a Galaxy * object. If this method were removed, then a copy constructor * would not be provided either, since immutable objects do not * need a copy constructor. */ public void setMass( double aMass ){ fMass = aMass; } public String getName() { return fName; } // PRIVATE ///// private double fMass; private final String fName; /** * Test harness. */ public static void main (String... aArguments){ Galaxy m101 = new Galaxy(15.0, "M101"); Galaxy m101CopyOne = new Galaxy(m101); m101CopyOne.setMass(25.0); System.out.println("M101 mass: " + m101.getMass()); System.out.println("M101Copy mass: " + m101CopyOne.getMass()); Galaxy m101CopyTwo = Galaxy.newInstance(m101); m101CopyTwo.setMass(35.0); System.out.println("M101 mass: " + m101.getMass()); System.out.println("M101CopyTwo mass: " + m101CopyTwo.getMass()); } }
Example run of this class :
>java -cp . Galaxy
M101 mass: 15.0
M101Copy mass: 25.0
M101 mass: 15.0
M101CopyTwo mass: 35.0
发表评论
-
Facade vs Proxy
2010-04-02 08:36 1188Question: i feel there is not m ... -
UML中关联(association)和依赖(dependency)的区别
2010-03-30 08:56 3211这个问题不仅我们这些小喽罗会迷惑,很多大拿们也没有统一的认识, ... -
Aggregation v.s. Composition
2010-03-30 08:13 1349Aggregation Aggregation is a k ... -
UML中几种类间关系:继承、实现、依赖、关联、聚合、组合
2009-12-06 23:13 1189继承,泛化(Generalization) 指的是一个类( ... -
Build模式的理解
2009-11-26 23:23 2150Builder模式 是为了将构建复杂对象的组装过程和它的创建部 ... -
设计模式简图
2009-11-26 22:00 856... -
Proxy, Builder and Decoration 异同点
2009-11-09 13:52 0晚上写分打发打发法 -
面向对象设计原则
2009-11-07 09:43 706正如牛顿三大定律 ... -
Flyweight Pattern 享元模式
2009-09-29 16:07 871GOF:运用共享技术有效地支持大量细粒度的对象。 解释一 ... -
Using the Prototype pattern to clone objects
2009-09-27 15:48 890Prototype pattern is one of the ... -
Is a Java Immutable Class Always final?
2009-09-27 15:10 1486In response to my recent blog p ... -
Defensive copying
2009-09-27 14:46 970A mutable object is simply an o ... -
Immutable objects
2009-09-27 14:13 849Immutable objects are simply ob ... -
Java Immutable Class
2009-09-27 14:09 1983Java Immutable Class[ From ] ... -
Value Object vs. Data Transfer Object (VO vs. DTO)
2009-09-18 10:31 1626The pattern which is known toda ...
相关推荐
operator overloading and copy constructors 英文原版。英国大学英文原本教材
I get asked this question sometimes from seasoned programmers who are new to C++. There are plenty of good books written on the subject, but I found no clear and concise set of rules on the Internet ...
Classes Doing Work in Constructors Default Constructors Explicit Constructors Copy Constructors Structs vs. Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control ...
Copy constructors, assignment operators, and exception safe assignment, How to use tags, Converting numbers to strings and strings to numbers, How To: Ask Questions The Smart Way ), Forum( 说明:...
Immutability, copy constructors, and lenses Combining FP paradigms with OOP, FRP, and POP in your day-to-day development activities Developing a backend application with Swift Developing an iOS ...
Understand the importance of immutability, copy constructors, and lenses Develop a backend API with Vapor Create an iOS app by combining FP, OOP, FRP, and POP paradigms In Detail Swift is a multi-...
Understand the importance of immutability, copy constructors, and lenses Develop a backend API with Vapor Create an iOS app by combining FP, OOP, FRP, and POP paradigms In Detail Swift is a multi-...
constructors, destructors, RTTI, and templates. Thus, Part Two covers those constructs that “make C++, C++.” Part Three describes the standard function library and Part Four examines the standard ...
如果自己没有申明,编译器会为我们提供一个copy构造函数、一个copy assignment操作符和一个析构函数。此外,如果没有申明任何构造函数,编译器会为我们申明一个default构造函数。很像下面的Empty类: class Empty{ ...
Copy Constructor and Object Cloning Class Member Access Class member offsets Function Pointers Function Shadowing Understanding the Destructor Operator Overloading Multiple Inheritance Casting ...
第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...
第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...
第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...
9.5 Constructors 9.6 Using the Student_info class 9.7 Details Chapter 10 Managing memory and low-level data structures 10.1 Pointers and arrays 10.2 String literals revisited 10.3 Initializing ...
merge upstream, Copy cache header for 304 response 1.0.6 2014.09.10 merge pr, fixed bug where Disk cache misses for redirects merge upstream, Fix crash/OOM in DiskBasedCache merge upstream, ...
Start using Pascal Script now and download your free copy, including full source code. Pascal Script for Delphi Pascal Script is a free scripting engine that allows you to use most of the Object ...
The Code Assistant now includes an <All> choice for default object type constructors. The Code Assistant can now include synonyms for user object lists. The Code Assistant no longer pre-selects <All> ...
The Code Assistant now includes an <All> choice for default object type constructors. The Code Assistant can now include synonyms for user object lists. The Code Assistant no longer pre-selects <All> ...
structuring of inline constructor syntax for all widgets that provide inline constructors * Signals and Slots event mechanism now widely used throughout the API * new configuration directives * new ...
// Constructors // construct an uninitialized size CSize(); // create from two integers CSize(int initCX, int initCY); // create from another size CSize(SIZE initSize); // create from a point ...