Solidity 学习笔记
通过学习Solidity
,然后输出文章检验自己的学习成果Github仓库
欢迎大家关注我的X
Solidity
中可用的值类型
true
和 false
或运算 |
&& 和 | 运算符遵循短路规则,a | b,如果a是true,b不会被计算;a&&b,如果a是false,b不会被计算 |
uint
代表无符号整数,也就是说没有负数的整数uint8
范围 从 0 到 $2^8-1$uint16
范围 从 0 到 $2^{16}-1$8
位为递增uint256
范围 从 0 到 $2^{256}-1$int
表示有符号整数,在int中允许有负数
存在int
表示的范围与uint
表示的范围不同
int8
范围 从 $-2^7$ 到 $2^7-1$int16
范围 从 $-2^{15}$ 到 $2^{15}-1$8
位为递增int256
范围 从 $-2^{255}$ 到 $2^{255}-1$20
字节的值call
, delegatecall
和 staticcall
成员方法transfer
和send
两个成员方法用于转账,和一个成员变量balance
用于查询余额bytes
表示一个字节序列Solidity
提供了两种字节类型:
bytes1, bytes2, bytes3, ..., bytes32
代表从1
到32
的字节序列bytes
类型的运算符:
位运算符: &, | , ^ (按位异或), ~ (按位取反) |
x
是bytesI
类型,那么当0 <= k < I
时,x[k]
返回第k
个字节(只读).length
: 这个字节数组的长度(只读)enum
枚举名称 { 枚举值1, 枚举值2, … }uint8
,从0
开始递增,不能超过256
个uint8
进行相互转换,并且会检查所转换的无符号整数是否在枚举长度范围内布尔类型(bool)
为false
整数类型(uint、int)
为0
地址类型(address)
为0x0000000000000000000000000000000000000000
定长字节数组类型(bytes)
为0
,根据长度决定,比如bytes2
为0x0000
枚举类型(enum)
为0
该例子是值类型使用的例子
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
contract ValueType {
bool public b = false;
bool public b1 = 1118 > 1114; // true
bool public b2 = 1118 >= 1114; // true
bool public b3 = 1118 < 1114; // false
bool public b4 = 1118 <= 1114; // false
bool public b5 = 1118 == 1114; // false
bool public b6 = 1118 != 1114; // true
bool public b7 = !b; // true
bool public b8 = b1 && b2; // true
bool public b9 = b3 && b1; // false,发生短路运算
bool public b10 = b3 || b4; // false
bool public b11 = b1 || b3; // true,发生短路运算
uint8 public u8 = 18;
uint24 public u24 = 1118;
uint public u256 = 1114; // uint是uint256的别称
int8 public i8 = - 18;
int24 public i24 = 1118;
int public i256 = - 1114; // int是int256的别称
int public minInt = type(int).min;
int public maxInt = type(int).max;
int public int_1 = - 1114;
int public int_2 = 1118 + 1114;
int public int_3 = 1118 - 1114;
int public int_4 = 18 * 14;
int public int_5 = 18 / 18;
int public int_6 = 1118 % 1114;
int public int_7 = 2 ** 10;
address public addr;
constructor ()payable{
}
function getEth() public {
addr = msg.sender;
address payable _addr = payable(addr);
_addr.transfer(1 ether);
}
bytes1 public a1 = 0xb5; // [10110101]
bytes2 public a2 = 0xb556; // [1011010101010110]
bytes1 public a3 = a1[0]; // [10110101]
enum Status{
Pending,
Shipped,
Accepted,
Rejected,
Canceled
}
Status public status;
Status public maxEnum = type(Status).max;
Status public minEnum = type(Status).min;
function get() public view returns (Status){
return status;
}
function set(Status _status) public {
status = _status;
}
function cancel() public {
status = Status.Canceled;
}
function reject() public {
status = Status.Rejected;
}
function accept() public {
status = Status.Accepted;
}
function ship() public {
status = Status.Shipped;
}
function pending() public {
status = Status.Pending;
}
function reset() public {
delete status;
delete a1;
delete b1;
delete u8;
delete i8;
delete addr;
}
bool public defaultBoo; // false
uint public defaultUint; // 0
int public defaultInt; // 0
address public defaultAddr; // 0x0000000000000000000000000000000000000000
bytes2 public defaultBytes; // 0x0000
Status public defaultEnum; // 0
}
bool public b = false;
bool public b1 = 1118 > 1114; // true
bool public b2 = 1118 >= 1114; // true
bool public b3 = 1118 < 1114; // false
bool public b4 = 1118 <= 1114; // false
bool public b5 = 1118 == 1114; // false
bool public b6 = 1118 != 1114; // true
bool public b7 = !b; // true
bool public b8 = b1 && b2; // true
bool public b9 = b3 && b1; // false,发生短路运算
bool public b10 = b3 || b4; // false
bool public b11 = b1 || b3; // true,发生短路运算
uint8 public u8 = 18;
uint24 public u24 = 1118;
uint public u256 = 1114; // uint是uint256的别称
int8 public i8 = - 18;
int24 public i24 = 1118;
int public i256 = - 1114; // int是int256的别称
int public minInt = type(int).min;
int public maxInt = type(int).max;
int public int_1 = - 1114;
int public int_2 = 1118 + 1114;
int public int_3 = 1118 - 1114;
int public int_4 = 18 * 14;
int public int_5 = 18 / 18;
int public int_6 = 1118 % 1114;
int public int_7 = 2 ** 10;
布尔类型
的例子address public addr;
addr
constructor ()payable{
}
constructor
表示构造函数,只会在合约部署的时候,调用一次payable
修饰,保证部署合约的时候,能打入eth
function getEth() public {
addr = msg.sender;
address payable _addr = payable(addr);
_addr.transfer(1 ether);
}
ether
到自己的账户,所以部署合约的时候,一定打入一个ether
以上,否则调用这个函数会失败bytes1 public a1 = 0xb5; // [10110101]
bytes2 public a2 = 0xb556; // [1011010101010110]
bytes1 public a3 = a1[0]; // [10110101]
enum Status{
Pending,
Shipped,
Accepted,
Rejected,
Canceled
}
Status public status;
Status public maxEnum = type(Status).max;
Status public minEnum = type(Status).min;
type(Status).max
和 type(Status).min
,可以得到枚举类型(enum)的最大值和最小值function get() public view returns (Status) {
return status;
}
function set(Status _status) public {
status = _status;
}
function cancel() public {
status = Status.Canceled;
}
Canceled
function reject() public {
status = Status.Rejected;
}
Rejected
function accept() public {
status = Status.Accepted;
}
Accepted
function ship() public {
status = Status.Shipped;
}
Shipped
function pending() public {
status = Status.Pending;
}
Pending
function reset() public {
delete status;
delete a1;
delete b1;
delete u8;
delete i8;
delete addr;
}
delete
关键字,可以将该变量,恢复成默认值bool public defaultBoo; // false
uint public defaultUint; // 0
int public defaultInt; // 0
address public defaultAddr; // 0x0000000000000000000000000000000000000000
bytes2 public defaultBytes; // 0x0000
Status public defaultEnum; // 0