15-Solidity8.0构造函数&事件

Solidity8.0

15-Solidity8.0构造函数&事件


在这里插入图片描述


前言

构造函数
constructor是在合约创建时执行的可选函数。

事件
Events允许登录到以太坊区块链。事件的一些用例是:

监听事件和更新用户界面
一种廉价的存储方式
// 事件声明
// 最多可以有3个参数被索引。
// 被索引的参数可以帮助你通过被索引的参数过滤日志


一、Solidity构造函数&事件

1.构造函数&事件

代码如下(示例):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8;

// Base contract X
contract X {
    string public name;
    event AnotherLog();
	event Log(address indexed sender, string message);
    constructor(string memory _name) {
        name = _name;
    }
    
	function test() public {
        emit Log(msg.sender, "Hello World!");
        emit AnotherLog();
    }
}

// Base contract Y
contract Y {
    string public text;

    constructor(string memory _text) {
        text = _text;
    }
}

// There are 2 ways to initialize parent contract with parameters.
// Pass the parameters here in the inheritance list.
contract B is X("Input to X"), Y("Input to Y") {

}

contract C is X, Y {
    // Pass the parameters here in the constructor,
    // similar to function modifiers.
    constructor(string memory _name, string memory _text) X(_name) Y(_text) {}
}

// Parent constructors are always called in the order of inheritance
// regardless of the order of parent contracts listed in the
// constructor of the child contract.

// Order of constructors called:
// 1. X
// 2. Y
// 3. D
contract D is X, Y {
    constructor() X("X was called") Y("Y was called") {}
}

// Order of constructors called:
// 1. X
// 2. Y
// 3. E
contract E is X, Y {
    constructor() Y("Y was called") X("X was called") {}
}

总结

日拱一卒。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值