Solidity-Learning

Solidity 学习笔记

View the Project on GitHub XdpCs/Solidity-Learning

003-值类型

背景

通过学习Solidity,然后输出文章检验自己的学习成果Github仓库

欢迎大家关注我的X

基础知识

布尔类型(bool)

整数类型(uint、int)

uint

int

整数类型(uint、int)的运算符

地址类型(address)

定长字节数组类型(bytes)

枚举类型(enum)

默认值

例子

例子

该例子是值类型使用的例子

// 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)使用的例子

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,发生短路运算

整数类型(uint、int)使用的例子

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)使用的例子

address public addr;
constructor ()payable{

}
function getEth() public {
    addr = msg.sender;
    address  payable _addr = payable(addr);
    _addr.transfer(1 ether);
}

定长字节数组类型(bytes)使用的例子

bytes1 public a1 = 0xb5; // [10110101]
bytes2 public a2 = 0xb556; // [1011010101010110]
bytes1 public a3 = a1[0]; // [10110101]

枚举类型(enum)使用的例子

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

链接