From f55ed8536ef8caab5ddc5718fd257012398a1197 Mon Sep 17 00:00:00 2001 From: qyx <565485304@qq.com> Date: Mon, 17 Jul 2023 16:55:01 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=80=E4=BA=9B=E5=AD=A6?= =?UTF-8?q?=E4=B9=A0=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 29 ++++++++++++++++++++++++----- demo/ArrayTest.sol | 30 ++++++++++++++++++++++++++++++ demo/solidity1.sol | 14 ++++++++++++++ demo/testaddr.sol | 19 +++++++++++++++++++ 4 files changed, 87 insertions(+), 5 deletions(-) create mode 100644 demo/ArrayTest.sol create mode 100644 demo/testaddr.sol diff --git a/README.md b/README.md index 8bfcf5a..ed063ba 100644 --- a/README.md +++ b/README.md @@ -233,11 +233,30 @@ personal.listAccounts - 位运算符 & | ^ `` - 算术运算 + - * / % ** << >> - 右移 等价于 除法去除以2的几次方 - - - - 字符串常量(String literals) - - 地址类型(Address) - - + - 常量(literals) + - 有理数和整型常量 + - 有理数的常量计算不会有溢出的 + - 字符串常量 + - 十六进制常量 + - 地址常量 + - 地址合法性检查 + - 地址类型(Address) + - address: 表示一个账户地址(20个字节) + - 成员: 属性 balance, 函数 transfer() + +- 引用类型 + - 数据位置(Data location) + - memory, storage + - 数组类型 + - 数组(Arrays) + - T[k]: 元素类型为T, 固定长度为k的数组 + - T[]: 元素类型为T, 长度动态调整 + - bytes string 是一种特殊的数组 + - string 可转为 bytes, bytes 类似 byte[] + - 数组类型 + - 属性:length + - 函数: push() + - diff --git a/demo/ArrayTest.sol b/demo/ArrayTest.sol new file mode 100644 index 0000000..df63d13 --- /dev/null +++ b/demo/ArrayTest.sol @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.0; + +contract ArrayTest { + + // 以下标为定义的访问器 1 => 2 + uint [] public u = [1, 2, 3]; + + string s = "abcdefg"; + + // 获取数组的长度 => string 可以转换成 bytes + function h() public constant returns (uint) { + return bytes(s).length; // 7 + } + + // 返回下标为1的元素 + function f() public view returns (byte) { + + // 调用下面的数组 + g([uint(1), 2, 3]); + + return bytes(s)[1]; // b -> asc 码 0x62 + } + + // 数组作为参数进行传递 + function g(uint[3] _data) public constant { + + } + +} diff --git a/demo/solidity1.sol b/demo/solidity1.sol index 1678c25..3dabc91 100644 --- a/demo/solidity1.sol +++ b/demo/solidity1.sol @@ -7,9 +7,23 @@ import "solidity_for_import.sol"; contract Test{ // 状态变量定义 uint a; + int256 b = 20; + int256 c = 30; + bool boola = true; bool boolb = false; + function testInt() public returns (int) { + if (b > c){ + return b + c; + }else if(b == c){ + return b * c; + }else { + return b << 2; // b / 2^2 + } + -1 >> 2; + } + // 函数定义 diff --git a/demo/testaddr.sol b/demo/testaddr.sol new file mode 100644 index 0000000..2258802 --- /dev/null +++ b/demo/testaddr.sol @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.4.16; + +contract TestAddr { + + function deposit() public payable { + + } + + /* 获取余额 */ + function getBalance() public constant returns (uint) { + return this.balance; + } + + /* 转移以太 */ + function transferEther(address towho) public { + towho.transfer(10); // send 方法可能会失败 + } +}