二、New操作符

pragma solidity 0.4.8; 

/*
* @title Example the for Solidity Course
* @author Ethereum Community
*/

// A contract can create a new contract using the new keyword. 
// The full code of the contract being created has to be known in advance, 
// so recursive creation-dependencies are not possible.

// lets define a simple contract D with payable modifier
contract D {
    uint x;
    function D(uint a) payable {
        x = a;
    }
}


contract C {
    // this is how you can create a contract using new keyword
    D d = new D(4); 
    // this above line will be executed as part of C's constructor

    // you can also create a contract which is already defined inside a function like this
    function createD(uint arg) {
        D newD = new D(arg);
    }


    // You can also create a conract and at the same time transfer some ethers to it.
    function createAndEndowD(uint arg, uint amount) {
        // Send ether along with the creation of the contract
        D newD = (new D).value(amount)(arg);
    }
}

感谢您对本文的关注,如果您对区块链技术有兴趣,可以加入我们一起探讨, 请扫码关注“可可链”的微信公众号,并留言“加入可可链”。

本文欢迎转载,转载时请注明本文来自 微信公众号“可可链”。