"Solidity" 2. Contract structure-deep understanding of solidity

Source: Internet
Author: User
Tags modifier modifiers visibility
Index"Solidity" 1. The layout of a solidity source file "solidity" 2. Structure of the contract
"Solidity" 3. Type "solidity" 4. Unit and global variable "solidity" 5. Expression and control structure "solidity" 6. Contract
"Solidity" 7. Part "solidity" 8. Miscellaneous 2 structure of the contract

Solidity contracts are similar to classes in object-oriented languages. Each contract can contain state Variables, functions, Function modifiers, Events, Structs Types, and Enum Types declarations. In addition, contracts can be inherited from other contracts. State Variables

A state variable is a value that is permanently stored in the contract store.

pragma solidity ^0.4.0;

Contract Simplestorage {
    uint Storeddata;//state variable
    //...
}

See the "Type" section, "Visibility", and "get" on the type of state variable to get a possible selection of visibility. function Functions

A function is an executable unit in a code contract.

pragma solidity ^0.4.0;

Contract Simpleauction {function
    bid () payable {//function
        //...
    }
}

Function calls can occur either internally or externally, with varying degrees of visibility on other contracts (visibility and getter). function modifiers function modifiers

function modifiers can be used to declaratively modify the semantics of a function (see the function modifiers in the contract section).

pragma solidity ^0.4.11;

Contract Purchase {address public
    seller;

    Modifier Onlyseller () {//Modifier
        require (msg.sender = seller);
        _;
    }

    function abort () Onlyseller {//Call modifier
        //...
    }
}
Events

Event is an easy interface with the EVM log tool.

pragma solidity ^0.4.0;

Contract Simpleauction {Event
    highestbidincreased (address bidder, uint amount);//Events

    function bid () payable {
        // ...
        Highestbidincreased (Msg.sender, Msg.value); Triggering event
    }
}

See the events section of the contract for information about how events are declared and can be used within DAPP. Structure Type

Structs is a custom type that can group several variables (see the structure body in the type part).

pragma solidity ^0.4.0;

Contract ballot {
    struct voter {//struct body
        uint weight;
        BOOL voted;
        Address delegate;
        UINT Vote
    }
}
Enum Type

Enumeration can be used to create a custom type with a finite set of values (see enumerations in the Type section).

pragma solidity ^0.4.0;

Contract Purchase {
    enum state {Created, Locked, Inactive}//enum
}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.