Skip to content

Commit ad28196

Browse files
committed
Add study notes for 2025-08-14
1 parent e516fac commit ad28196

1 file changed

Lines changed: 141 additions & 0 deletions

File tree

Sillyzhe.md

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

1717
<!-- Content_START -->
18+
# 2025-08-14
19+
20+
### 5 继承
21+
22+
Solidity支持合约之间的继承,一个合约可以继承另一个合约的所有属性和方法。
23+
24+
例如:
25+
26+
```solidity
27+
contract A {
28+
uint public num = 100;
29+
}
30+
31+
contract B is A {
32+
function getNum() public view returns (uint) {
33+
return num;
34+
}
35+
}
36+
```
37+
38+
也支持多个合约继承,多重继承,例如C继承B和A。
39+
40+
```solidity
41+
42+
contract A {
43+
uint public num = 100;
44+
}
45+
46+
contract B {
47+
uint public num = 200;
48+
}
49+
50+
contract C is A, B {
51+
function getNum() public view returns (uint, uint) {
52+
return (num, num);
53+
}
54+
}
55+
56+
```
57+
58+
如果需要对父合约中的方法重写,可以使用`override`关键字,能被重用的合约方法需要使用`virtual`关键字。
59+
60+
```solidity
61+
62+
调用父合约的方法,有2中方式,一种是通过父合约的合约名调用,另一种是通过super关键字调用。
63+
64+
- 通过父合约的合约名调用
65+
parentContract.functionName();
66+
- 通过super关键字调用,会调用最近的父类的functionName方法
67+
super.functionName();
68+
69+
```solidity
70+
71+
contract A {
72+
function doSomething() public virtual {
73+
// A
74+
}
75+
}
76+
77+
contract B is A {
78+
function doSomething() public override {
79+
// B
80+
// B中的doSomething方法会覆盖A中的doSomething方法
81+
82+
// B调用A中的doSomething方法
83+
super.doSomething();
84+
A.doSomething();
85+
}
86+
}
87+
88+
```
89+
90+
### 6 错误处理
91+
92+
错误处理有三种方式,`require``revert``assert`
93+
94+
#### 6.1 assert
95+
96+
断言是一种用来检查合约状态的机制,如果断言失败,合约会立即停止执行。
97+
98+
```solidity
99+
100+
contract MyContract {
101+
function doSomething() public {
102+
assert(1 == 2);
103+
}
104+
}
105+
106+
```
107+
108+
#### 6.2 require
109+
110+
```solidity
111+
112+
contract MyContract {
113+
function doSomething() public {
114+
require(1 == 2, "1 is not equal to 2");
115+
}
116+
}
117+
118+
```
119+
120+
#### 6.3 revert
121+
122+
revert是一种用来回滚交易的机制,如果revert被调用,交易会被回滚。
123+
124+
```solidity
125+
126+
contract MyContract {
127+
function doSomething() public {
128+
if (1 != 2) {
129+
revert("Something went wrong");
130+
}
131+
}
132+
}
133+
134+
```
135+
136+
#### 6.4 Error
137+
138+
error是solidity 0.8.4版本新加的内容,方便且高效(省gas)地向用户解释操作失败的原因,同时还可以在抛出异常的同时携带参数,帮助开发者更好地调试。人们可以在contract之外定义异常。下面,我们定义一个TransferNotOwner异常,当用户不是代币owner的时候尝试转账,会抛出错误:
139+
140+
```solidity
141+
error TransferNotOwner(address sender)
142+
143+
// error的使用必须搭配revert
144+
function transferOwner1(uint256 tokenId, address newOwner) public {
145+
if(_owners[tokenId] != msg.sender){
146+
// revert TransferNotOwner();
147+
revert TransferNotOwner(msg.sender);
148+
}
149+
_owners[tokenId] = newOwner;
150+
}
151+
```
152+
153+
### 错误处理的区别
154+
155+
* assert:用于检查合约的内部错误,如果断言失败,合约会立即停止执行。
156+
* require:用于检查合约的输入参数,如果条件不满足,合约会回滚。
157+
* revert:用于检查合约的状态,如果条件不满足,合约会回滚。
158+
18159
# 2025-08-13
19160

20161
### 1 函数

0 commit comments

Comments
 (0)