以太坊智能合约开发 Solidity学习

1.

pragma solidity >=0.4.22 <0.6.0;//版本号,头文件
contract BooleanTest
{
    bool _a;//默认返回false
    int num1=100;
    int num2=200;
    function getBool() public view returns (bool)
    {
        return _a;
    }
    function getBool2() public view returns (bool)
    {
        return !_a;//去反
    }
    function judge() public view returns (bool)
    {
        return num1==num2;
    }
    function judge2() public view returns (bool)
    {
        return num1!=num2;
    }
    // && ||
    function and() public view returns (bool)//
    {
        return (num1==num2)&&true;
    }
    function or() public view returns (bool)//
    {
        return (num1==num2)||true;
    }
    
}

2.

pragma solidity >=0.4.22 <0.6.0;
contract ByteArray{
    bytes1 public num1=0x7a;
    bytes2 public num2=0x7a68;//加上public后自动获得get方法
    function  getlength() public view returns(uint)
    {
        return num1.length;
    }
}

 3.

pragma solidity >=0.4.22 <0.6.0;
//动态字节数组
contract DynamicByteArray{
    bytes public name=new bytes(2);
    function InitName()public
    {
        name[0]=0x7a;
        name[1]=0x68;
    }
    function getlength()public view returns(uint)//获取动态长度
    {
        return name.length;
    }
    function ChangeName()public //修改内容
    {
        name[0]=0x88;
    }
    function ChangeLength()public//修改动态长度
    {
        name.length=5;
    }
    function pushtest()public//动态添加数据到动态字节数组之中,加载之后
    {
        name.push(0x99);
    }
}

 

你可能感兴趣的:(以太坊智能合约开发 Solidity学习)