Skip to main content

变量类型-初始值

1-1. 数值类型(Value Type):这类变量赋值时候直接传递数值

a.整数类型:

  • uint: 无符号整数类型,表示非负整数。
  • int: 有符号整数类型,可以表示正整数、负整数或零。
  • uint8 到 uint256 和 int8 到 int256: 具体位数的整数类型,例如 uint8 表示一个8位无符号整数。 solidity中最常用的变量类型是uint,也就是正整数,取到负值的话,会报underflow错误。
// 整型
int public _int = -1; // 整数,包括负数
uint public _uint = 1; // 正整数
uint256 public _number = 20220330; // 256位正整数

// 整数运算
uint256 public _number1 = _number + 1; // +,-,*,/
uint256 public _number2 = 2**2; // 指数
uint256 public _number3 = 7 % 2; // 取余数
bool public _numberbool = _number2 > _number3; // 比大小

b.bool

// 布尔值
bool public _bool = true;

// 布尔运算
bool public _bool1 = !_bool; //取非
bool public _bool2 = _bool && _bool1; //与
bool public _bool3 = _bool || _bool1; //或
bool public _bool4 = _bool == _bool1; //相等
bool public _bool5 = _bool != _bool1; //不相等

c.地址类型:

address: 存储 20 字节的 Ethereum 地址。

地址类型也有成员变量,并作为所有合约的基础。

有普通的地址和可以转账ETH的地址(payable)。其中,payable修饰的地址相对普通地址多了transfer和send两个成员。在payable修饰的地址中,send执行失败不会影响当前合约的执行(但是返回false值,需要开发人员检查send返回值)。

balance和transfer(),可以用来查询ETH余额以及安全转账(内置执行失败的处理)。

// 地址
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address,可以转账、查余额
// 地址类型的成员
uint256 public balance = _address1.balance; // balance of address

d.枚举类型:

enum: 用于创建用户定义的枚举类型,可以表示一组命名的常量。

e.固定点数类型:

  • ufixed: 无符号固定点数类型。
  • fixed: 有符号固定点数类型。

1-2. 引用类型(Reference Type):包括数组和结构体

引用类型允许开发者在 Solidity 合约中处理更灵活的数据结构,以满足各种复杂的应用场景。这些类型通常用于存储和操作大量数据,例如用户信息、交易记录等。使用引用类型时,开发者需要注意 gas 成本和存储的限制,以确保合约的效率和可行性。

这类变量占空间大,赋值时候直接传递地址(类似指针)。

在 Solidity 中,通常不将数组(包括动态数组和固定大小数组)和字符串视为引用类型。相反,它们被视为值类型,因为它们在传递时是按值传递的,而不是按引用传递。

在 Solidity 中,引用类型主要是结构体(struct)、映射(mapping)、引用数据类型(storage 和 memory 引用)以及引用数据类型的数组。

  • 结构体(Struct):
struct Person {
string name;
uint age;
}
  • 映射(Mapping):Solidity里的哈希表
mapping(address => uint) balances;
  • 引用数据类型的数组:
Person[] public people;

1-3.数组,也属于引用类型

  • uint[3]: 固定大小的无符号整数数组,长度为3。
  • int[5]: 固定大小的有符号整数数组,长度为5。
  • bytes32[2]: 固定大小的字节数组,每个元素长度为32字节。

1-4.定长字节数组

字节数组bytes分两种,一种定长(byte, bytes8, bytes32),另一种不定长。定长的属于数值类型,不定长的是引用类型(之后讲)。 定长bytes可以存一些数据,消耗gas比较少。

// 固定长度的字节数组
bytes32 public _byte32 = "MiniSolidity";
bytes1 public _byte = _byte32[0];

MiniSolidity变量以字节的方式存储进变量_byte32,转换成16进制为:0x4d696e69536f6c69646974790000000000000000000000000000000000000000

_byte变量存储_byte32的第一个字节,为0x4d。

1-4.函数类型(Function Type):

Solidity文档里把函数归到数值类型

2.在solidity中,声明但没赋值的变量都有它的初始值或默认值

  • boolean: false
  • string: ""
  • int: 0
  • uint: 0
  • enum: 枚举中的第一个元素
  • address: 0x0000000000000000000000000000000000000000 (或 address(0))
  • function
    • internal: 空白方程
    • external: 空白方程

2-2.初值例子

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

contract InitialValue {
// Value Types
bool public _bool; // false
string public _string; // ""
int public _int; // 0
uint public _uint; // 0
address public _address; // 0x0000000000000000000000000000000000000000

enum ActionSet { Buy, Hold, Sell}
ActionSet public _enum; // 第一个元素 0

function fi() internal{} // internal空白方程
function fe() external{} // external空白方程

// Reference Types
uint[8] public _staticArray; // 所有成员设为其默认值的静态数组[0,0,0,0,0,0,0,0]
uint[] public _dynamicArray; // `[]`
mapping(uint => address) public _mapping; // 所有元素都为其默认值的mapping
// 所有成员设为其默认值的结构体 0, 0
struct Student{
uint256 id;
uint256 score;
}
Student public student;

// delete操作符
bool public _bool2 = true;
function d() external {
delete _bool2; // delete 会让_bool2变为默认值,false
}
}

引用类型初始值

  • 映射mapping: 所有元素都为其默认值的mapping
  • 结构体struct: 所有成员设为其默认值的结构体
  • 数组array
    • 动态数组: []
    • 静态数组(定长): 所有成员设为其默认值的静态数组

3.delete操作符

delete a会让变量a的值变为初始值。

// delete操作符
bool public _bool2 = true;

function d() external {
delete _bool2; // delete 会让_bool2变为默认值,false
}

变量类型例子

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract ValueTypes{
// 布尔值
bool public _bool = true;
// 布尔运算
bool public _bool1 = !_bool; //取非
bool public _bool2 = _bool && _bool1; //与
bool public _bool3 = _bool || _bool1; //或
bool public _bool4 = _bool == _bool1; //相等
bool public _bool5 = _bool != _bool1; //不相等

// 整数
int public _int = -1;
uint public _uint = 1;
uint256 public _number = 20220330;
// 整数运算
uint256 public _number1 = _number + 1; // +,-,*,/
uint256 public _number2 = 2**2; // 指数
uint256 public _number3 = 7 % 2; // 取余数
bool public _numberbool = _number2 > _number3; // 比大小


// 地址
address public _address = 0x7A58c0Be72BE218B41C608b7Fe7C5bB630736C71;
address payable public _address1 = payable(_address); // payable address,可以转账、查余额
// 地址类型的成员
uint256 public balance = _address1.balance; // balance of address


// 固定长度的字节数组
bytes32 public _byte32 = "MiniSolidity"; // bytes32: 0x4d696e69536f6c69646974790000000000000000000000000000000000000000
bytes1 public _byte = _byte32[0]; // bytes1: 0x4d


// Enum
// 将uint 0, 1, 2表示为Buy, Hold, Sell
enum ActionSet { Buy, Hold, Sell }
// 创建enum变量 action
ActionSet action = ActionSet.Buy;

// enum可以和uint显式的转换
function enumToUint() external view returns(uint){
return uint(action);
}
}