以太坊solidity学习记录(二)基础操作整理

1.solidity的四种可见度/访问权限

public:任何人都可以调用该函数,包括DApp的使用者。
private:只有合约本身可以调用该函数(在另一个函数中)。
internal:只有这份合同以及由此产生的所有合同才能称之为合同。
external:只有外部可以调用该函数,而合约内部不能调用。

2.solidity的三种修饰符

view: 可以自由调用,因为它只是“查看”区块链的状态而不改变它
pure: 也可以自由调用,既不读取也不写入区块链
payable:常常用于将代币发送给合约地址。

来源自solidity[1]-HelloWorld

3.一个solidity的函数应有以下部分组成

function
你的函数名字(类型1 名字,类型2 名字,。。。,类型n 名字) 如果没有就什么都不填即可
可见度/访问权限,即public/private/internal/external 如果不写则系统默认为public并且警告
修饰符,即view/pure/payable 如果需要花费gas则不写
returns(类型1 名字,类型2 名字,。。。,类型n 名字) PS.如果有的话

注意:调用函数时如果需要输入字符串,则在输入框之内需要将字符串用双引号括起来。
尝试了一下最新的网页版不加双引号也可以成功,可能是更新之后新增的功能,具体见图

pragma solidity ^0.4.0;

contract helloworld {
    function stringtest(string inputstr) public view returns(string){
        return inputstr;
    }
}

在这里插入图片描述

4.布尔类型

pragma solidity ^0.4.0;

contract helloworld {
    bool boola=true; //声明一个布尔类型的值,只用一个等号
    function booltesta() public view returns(bool){
        return boola;
    }
    
    function booltestb(int a,int b) public view returns(bool){
        return a==b;
    }
}

测试结果
在这里插入图片描述

5.与,或

即&&,||

pragma solidity ^0.4.0;

contract helloworld {
    function andtestTT() public view returns(bool){
        return true&&true;
    }
    function andtesTF() public view returns(bool){
        return true&&false;
    }
    function andtestFF() public view  returns(bool){
        return false&&false;
    }
    function ortestTT() public view returns(bool){
        return true||true;
    }
    function ortesTF() public view  returns(bool){
        return true||false;
    }
    function ortestFF() public view  returns(bool){
        return false||false;
    }
}

测试结果
在这里插入图片描述

6.通常运算符

即+,-,*,/,%以及特殊的符号**代表x的x次幂

pragma solidity ^0.4.0;

contract helloworld {
     function jiatest(int a,int b) public view  returns(int){
        return a+b;
    }
    function jiantest(int a,int b)  public view returns(int){
        return a-b;
    }
    function chengtest(int a,int b) public view  returns(int){
        return a*b;
    }
    function chutest(int a,int b)  public view returns(int){
        return a/b;
    }
    function quyutest(int a,int b)  public view returns(int){
        return a%b;
    }
    function mitest(uint a,uint b)  public view returns(uint){
        return a**b; //此处必须为uint,直接写int256会报错
    }
}

测试结果
在这里插入图片描述

7.位运算符

1.& 操作数之间转换成二进制之后每位进行与运算操作(同1取1)
2.| 操作数之间转换成二进制之后每位进行或运算操作(有1取1)
3.~ 操作数转换成二进制之后每位进行取反操作(直接相反)
4.^ 操作数之间转换成二进制之后每位进行异或操作(不同取1)
5.<<操作数转换成二进制之后每位向左移动x位的操作
6.>>操作数转换成二进制之后每位向右移动x位的操作
举例

pragma solidity ^0.4.0;

contract helloworld {
    function Wyutest(uint8 a,uint8 b)  public view returns(uint8){
        return a&b;
    }
    function Whuotest(uint8 a,uint8 b)  public view returns(uint8){
        return a|b;
    }
    function Wfantest(uint8 a)  public view returns(uint8){
        return ~a;
    }
    function Wyihuotest(uint8 a,uint8 b)  public view returns(uint8){
        return a^b;
    }
    function zuoyitest(uint8 a,uint8 b)  public view returns(uint8){
        return a<<b;
    }
    function youyitest(uint8 a,uint8 b)  public view returns(uint8){
        return a>>b;
    }
}

运行结果
在这里插入图片描述

8.solidity中赋值

solidity是先将赋值语句之中所有的都计算出来之后再进行赋值操作的
举例

pragma solidity ^0.4.0;

contract helloworld {
    function setvaluetest() public view returns(uint8){
        return 9999999999999999999-9999999999999999998;
    }
}

测试结果
在这里插入图片描述

9.固定长度字节数组byte

一个byte=8个位(XXXX XXXX)X为0或1,二进制表示
byte数组为bytes1,bytes2,。。。,bytes32,以八个位递增,即是对位的封装
举例
bytes1=uint8
bytes2=unit16



bytes32=unit256

使用byte数组的理由:

1.bytesX可以更好地显示16进制
举例:bytes1=0x6A,bytes1=(XXXX XXXX)正好四个表示一个16进制,以此类推
2.bytes数据声明时加入public可以自动生成调用长度的函数,见下

pragma solidity ^0.4.0;

contract helloworld {
    bytes1 public num1 = 0x12;  
    bytes4 public num2 = 0x12121212;
}

在这里插入图片描述

3.bytes内部自带length长度函数,而且长度固定,而且长度不可以被修改。见下

pragma solidity ^0.4.0;

contract helloworld {
    bytes1 public num1 = 0x12;  
    bytes4 public num2 = 0x12121212;
    function getlength1() public view returns(uint8){
        return num1.length;
    }
    function getlength2() public view returns(uint8){
        return num2.length;
}

在这里插入图片描述

4.字节数组可以进行大小比较

pragma solidity ^0.4.0;

contract helloworld {
    bytes1 public num1 = 0x12;  
    bytes4 public num2 = 0x12121212;
    uint8 num3 = 0x12;
    uint8 num4 = 12;
    function compare1() public view returns(bool){
        return num1<num2;
    }
    function compare2() public view returns(bool){
        return num1>num2;
    }
    function compare3() public view returns(bool){
        //return num1>num3;不管是16进制还是二进制,编译器都会报错,
        //return num1>num4;说明无法进行byte和int之间的比较
    }
}

10.可变长度byte数组

声明方法
bytes arr = new bytes(length);
举例

pragma solidity ^0.4.0;

contract helloworld {
    
     bytes arr1 = new bytes(3);
    function initarr() public view{
        arr1[0]=0x12;
        arr1[1]=0x34;
    }
    function getarrlength() public view returns(uint){
        return arr1.length;
    }
     function arrchange() public {
        arr1[0]=0x11; //
    }
    
     function arrlengthchange(uint a) public {
        arr1.length=a; //
    }
    
    function pushtest() public {
        arr1.push(0x56);
    }
}

其他注意:注意操作时不要出现位溢出的情况,如uint8中的数值超过255或者为负。还有除数为0等等常见错误

本文所有代码见下

pragma solidity ^0.4.0;

contract helloworld {
    
    function stringtest(string inputstr) public view returns(string){
        return inputstr;
    }
    bool boola=true; //声明一个布尔类型的值,只用一个等号
    function booltesta() public view returns(bool){
        return boola;
    }
    
    function booltestb(int a,int b) public view returns(bool){
        return a==b;
    }
    function andtestTT() public view returns(bool){
        return true&&true;
    }
    function andtesTF() public view returns(bool){
        return true&&false;
    }
    function andtestFF() public view  returns(bool){
        return false&&false;
    }
    function ortestTT() public view returns(bool){
        return true||true;
    }
    function ortesTF() public view  returns(bool){
        return true||false;
    }
    function ortestFF() public view  returns(bool){
        return false||false;
    }
     function jiatest(int a,int b) public view  returns(int){
        return a+b;
    }
    function jiantest(int a,int b)  public view returns(int){
        return a-b;
    }
    function chengtest(int a,int b) public view  returns(int){
        return a*b;
    }
    function chutest(int a,int b)  public view returns(int){
        return a/b;
    }
    function quyutest(int a,int b)  public view returns(int){
        return a%b;
    }
    function mitest(uint a,uint b)  public view returns(uint){
        return a**b; //此处必须为uint,直接写int256会报错
    }
    function Wyutest(uint8 a,uint8 b)  public view returns(uint8){
        return a&b;
    }
    function Whuotest(uint8 a,uint8 b)  public view returns(uint8){
        return a|b;
    }
    function Wfantest(uint8 a)  public view returns(uint8){
        return ~a;
    }
    function Wyihuotest(uint8 a,uint8 b)  public view returns(uint8){
        return a^b;
    }
    function zuoyitest(uint8 a,uint8 b)  public view returns(uint8){
        return a<<b;
    }
    function youyitest(uint8 a,uint8 b)  public view returns(uint8){
        return a>>b;
    }
    
    function setvaluetest() public view returns(uint8){
        return 9999999999999999999-9999999999999999998;
    }
    
    bytes1 public num1 = 0x12;  
    bytes4 public num2 = 0x12121212;
    uint8 num3 = 0x12;
    uint8 num4 = 12;
    function getlength1() public view returns(uint8){
        return num1.length;
    }
    function getlength2() public view returns(uint8){
        return num2.length;
    }
    function compare1() public view returns(bool){
        return num1<num2;
    }
    function compare2() public view returns(bool){
        return num1>num2;
    }
    function compare3() public view returns(bool){
        //return num1>num3;
        //return num1>num4;
    }
    
    
    bytes arr1 = new bytes(3);
    function initarr() public view{
        arr1[0]=0x12;
        arr1[1]=0x34;
    }
    function getarrlength() public view returns(uint){
        return arr1.length;
    }
     function arrchange() public {
        arr1[0]=0x11; //
    }
    
     function arrlengthchange(uint a) public {
        arr1.length=a; //
    }
    
    function pushtest() public {
        arr1.push(0x56);
    }
}
发布了9 篇原创文章 · 获赞 0 · 访问量 396

猜你喜欢

转载自blog.csdn.net/weixin_45067603/article/details/105738788