`
d02540315
  • 浏览: 31403 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
最近访客 更多访客>>
社区版块
存档分类
最新评论

Copy constructors

 
阅读更多
Copy constructors :

  •     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
分享到:
评论

相关推荐

    operator overloading and copy constructors 英文原版

    operator overloading and copy constructors 英文原版。英国大学英文原本教材

    Copy Constructors and Assignment Operators终极解释

    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 ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Classes Doing Work in Constructors Default Constructors Explicit Constructors Copy Constructors Structs vs. Classes Inheritance Multiple Inheritance Interfaces Operator Overloading Access Control ...

    c++官方网站标准参考资料

    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( 说明:...

    Swift 3 Functional Programming 【2016】

    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 ...

    Swift Functional Programming - Second Edition

    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-...

    Packt.Swift.Functional.Programming.2nd.Edition.2017

    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-...

    C++ The Complete Reference, 4th Edition - Copy

    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 ...

    浅谈C++ Explicit Constructors(显式构造函数)

    如果自己没有申明,编译器会为我们提供一个copy构造函数、一个copy assignment操作符和一个析构函数。此外,如果没有申明任何构造函数,编译器会为我们申明一个default构造函数。很像下面的Empty类: class Empty{ ...

    Selected.Topics.in.Cplusplus.15117

    Copy Constructor and Object Cloning Class Member Access Class member offsets Function Pointers Function Shadowing Understanding the Destructor Operator Overloading Multiple Inheritance Casting ...

    深度探索模C++对象模型PDF

    第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...

    深度探索C++对象模型 超清版

    第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    第2章 构造函数语意学(The Semantics of constructors) 2.1 Default Constructor的建构操作 “带有Default Constructor”的Member Class Object “带有Default Constructor”的Base Class “带有一个Virual ...

    Accelerated C++ Practical Programming by Example

    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 ...

    Github-volley 最新jar包和源代码 2015.07.28

    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, ...

    超强大、好用的Pascal语言解释器(RemObjects Pascal Script)

    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 ...

    plsqldev12.0.6.1832x32主程序+ v12中文包+keygen

    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> ...

    plsqldev12.0.6.1832x64主程序+ v12中文包+keygen

    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> ...

    netWindows_0.3.0_pre2

    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 ...

    stdafx.h代码

    // 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 ...

Global site tag (gtag.js) - Google Analytics