Solidity-008 GeneralStructure

本文详细描述了一个使用Pragmasolidity0.8.9编写的智能合约,包括状态变量、结构体、修饰器、事件和函数,展示了如何在智能合约中管理数据和执行逻辑。
摘要由CSDN通过智能技术生成

pragma solidity ^0.8.9;

// Contract definition

contract GeneralStructure {

    // State variables

    int public stateIntVariable; // An integer state variable

    string stateStringVariable; // A string state variable

    address personIdentifier; // An address state variable

    MyStruct human; // A state variable of structure type MyStruct

    bool constant hasIncome = true; // A constant boolean state variable

    // Structure definition

    struct MyStruct {

        string name; // A string variable within the struct

        uint myAge; // An unsigned integer variable within the struct

        bool isMarried; // A boolean variable within the struct

        uint[] bankAccountsNumbers; // A dynamic array of unsigned integers within the struct

    }

    // Modifier declaration

    modifier onlyBy() {

        if (msg.sender == personIdentifier) {

            _; // Execute the function if the sender matches personIdentifier

        }

    }

    // Event declaration

    event ageRead(address indexed _person, int _age); // Event to log age reading

    // Enumeration declaration

    enum gender {male, female} // Enumeration to represent gender

    // Function definition

    function getAge (address _personIdentifier) onlyBy() payable external returns (uint) {

        // Initialize the MyStruct instance with sample data

        human = MyStruct("Ritesh", 10, true, new uint[](3));

       

        // Assign the gender using the gender enumeration

        gender _gender = gender.male;

       

        // Emit the ageRead event to log age reading

        emit ageRead(personIdentifier, stateIntVariable);

       

        // Return a placeholder value (uint), this can be updated as needed

        return 0;

    }

}

//Deploy screenshoot show:

  • 15
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值