Skip to content

Commit 55fecd4

Browse files
committed
Add study notes for 2025-08-16
1 parent 8388433 commit 55fecd4

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

satalai.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,64 @@ timezone: UTC+8
1515
## Notes
1616

1717
<!-- Content_START -->
18+
# 2025-08-16
19+
20+
### 1、环境准备
21+
22+
- Remix IDE
23+
24+
### 2、合约代码
25+
26+
```solidity
27+
prama solidity ^0.8.0;
28+
```
29+
30+
Solidity开头的版本声明
31+
32+
```solidity
33+
contract MessageBoard {
34+
//保存所有人的留言记录
35+
mapping(address => string[]) public messages;
36+
37+
//留言事件,便于检索器和区块链浏览器追踪
38+
event NewMessage(address indexed sender,string message);
39+
40+
//构造函数,在部署时留言一条欢迎词
41+
constructor(){
42+
string memory initMsg = "Hello Eth Panda";
43+
messages[msg.sender].push(initMsg);
44+
emit NewMessage(msg.sender,initMsg);
45+
}
46+
47+
//发送一条留言
48+
function leaveMessage(string memory _msg)public {
49+
messages[msg.sender].push(_msg);
50+
emit NewMessage(msg.sender,_msg);
51+
}
52+
//查询某人第N条留言(从0开始)
53+
function getMessage(address user, unit256 index) public view returns (string memory)
54+
{
55+
return messages[user][index];
56+
}
57+
//查询某人一共发了多少条
58+
function getMessageCount(address user)public view (uint256)
59+
{
60+
return messages[user].length;
61+
}
62+
}
63+
```
64+
65+
- contract
66+
- 合约结构 (固定)
67+
- mapping(address => string[]) public messages;
68+
- mapping 映射数据类型
69+
- address ⇒ string 指的是一个地址映射到一个字符串数组
70+
- message映射变量名称
71+
- 可以通过messages[地址]访问该地址对应的字符串数组
72+
- event NewMessage(address indexed sender, string message);
73+
- event 事件结构(固定)
74+
- indexed表示这个sender参数被索引了,方便后续查找
75+
1876
# 2025-08-15
1977

2078
### 继承与函数重写

0 commit comments

Comments
 (0)