Skip to content

Commit 9940ef5

Browse files
committed
Add study notes for 2025-08-14
1 parent 98a96de commit 9940ef5

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

Ariko77.md

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

1717
<!-- Content_START -->
18+
# 2025-08-14
19+
20+
接口
21+
接口类似于抽象合约,但是它们不能实现任何函数。还有进一步的限制:
22+
无法继承其他合约,不过可以继承其他接口。
23+
所有的函数都需要是 external
24+
无法定义构造函数。
25+
无法定义状态变量。
26+
接口基本上基本上仅限于合约 ABI 可以表示的内容,并且 ABI 和接口之间的转换应该不会丢失任何信息。
27+
接口由它们自己的关键字表示:
28+
1
29+
2
30+
3
31+
4
32+
5
33+
6
34+
7
35+
pragma solidity >=0.6.2 <0.7.0;
36+
37+
interface Token {
38+
enum TokenType { Fungible, NonFungible }
39+
struct Coin { string obverse; string reverse; }
40+
function transfer(address recipient, uint amount) external;
41+
}
42+
就像继承其他合约一样,合约可以继承接口。接口中的函数都会隐式的标记为 virtual ,意味着他们会被重写。 但是不表示重写(overriding)函数可以再次重写,仅仅当重写的函数标记为 virtual 才可以再次重写。
43+
(继承去翻WTF笔记)
44+
接口可以继承其他的接口,遵循同样继承规则。
45+
1
46+
2
47+
3
48+
4
49+
5
50+
6
51+
7
52+
8
53+
9
54+
10
55+
11
56+
12
57+
13
58+
14
59+
pragma solidity >=0.6.2 <0.7.0;
60+
61+
interface ParentA {
62+
function test() external returns (uint256);
63+
}
64+
65+
interface ParentB {
66+
function test() external returns (uint256);
67+
}
68+
69+
interface SubInterface is ParentA, ParentB {
70+
// 必须重新定义 test 函数,以表示兼容父合约含义
71+
function test() external override(ParentA, ParentB) returns (uint256);
72+
}
73+
定义在接口或其他类合约( contract-like)结构体里的类型,可以在其他的合约里用这样的方式访问: Token.TokenType 或 Token.Coin.
74+
1875
# 2025-08-13
1976

2077
Fallout

0 commit comments

Comments
 (0)