区块链——Solidity智能合约基础实战(二)
Boolean及与、或、非
- 新建.sol文件,命名为Boolean.sol
- 编写合约
pragma solidity ^0.4.0;
contract BooleanTest{
bool _a;
function getBool() returns(bool){
return _a;
}
} |
- 部署,测试合约:发现未赋初值时,默认为false
在solidity中 ! 表示的是取反,未赋初值的变量加上!后表示true
本节代码
pragma solidity ^0.4.0;
contract BooleanTest{
bool _a; int num1 = 100; int num2 = 200;
function getBool() returns(bool){
return _a;
}
function getBool2() returns(bool){ return !_a; }
function check1() returns(bool){ return num1==num2; }
function check2() returns(bool){ return num1!=num2; }
// && || // true && false = false // false && true = false // true && true = true // true || false = true
function and1() returns(bool){ return (num1==num2) && true; }
function and2() returns(bool){ return (num1!=num2) && true; } function or1() returns(bool){ return (num1==num2)||true; } function or2() returns(bool){ return (num1!=num2) || true; } function or3() returns(bool){ return (num1==num2) || false; } } |
运行效果: