①属性、状态
②行为
pragma solidity ^0.5.0;
contract HelloWorld{
string Myname = "lynn";
function getName() public view returns(string){
return Myname;
}
function changeName(string _newName) public{
Myname = _newName;
}
}
错误信息:
(1)
browser/HelloWorld.sol:6:44: TypeError: Data location must be "memory" for return parameter in function, but none was given.
function getName() public view returns(string){
^----^
function getName() public view returns(string)
修改为:
function getName() public view returns(string memory)
(2)
browser/HelloWorld.sol:10:25: TypeError: Data location must be "memory" for parameter in function, but none was given.
function changeName(string _newName) public{
^-------------^
function changeName(string _newName) public
修改为:
function changeName(string memory _newName) public
修改后:
pragma solidity ^0.5.0;
contract HelloWorld{
string Myname = "lynn";
function getName() public view returns(string memory){
return Myname;
}
function changeName(string memory _newName) public{
Myname = _newName;
}
}
pragma solidity ^0.5.0;
contract HelloWorld{
string Myname = "lynn";
function getName() public view returns(string memory){
return Myname;
}
function changeName(string memory _newName) public{
Myname = _newName;
}
function pureTest(string memory _name) pure public returns(string memory){
return _name;
}
}
pragma solidity ^0.5.0;
contract BooleanTest{
bool _a;
int num1 = 100;
int num2 = 200;
function getBool() public view returns(bool){
return _a;
}
function getBool2() public view returns(bool){
return !_a;
}
function panduan() public view returns(bool){
return num1==num2;
}
function panduan2() public view returns(bool){
return num1!=num2;
}
function yu() public view returns(bool){
return (num1==num2) && true;
}
function yu2() public view returns(bool){
return (num1!=num2) && true;
}
function huo() public view returns(bool){
return (num1==num2) && true;
}
function huo2() public view returns(bool){
return (num1!=num2) && true;
}
function huo3() public view returns(bool){
return (num1==num2) && false;
}
}
pragma solidity ^0.5.0;
contract math{
// int uint
uint numa = 100;
uint numb = 200;
// uint numc = 2;
function add(uint a, uint b) pure public returns(uint){
return a+b;
}
function sub(uint a, uint b) pure public returns(uint){
return a-b;
}
function mul(uint a, uint b) pure public returns(uint){
return a*b;
}
function div(uint a, uint b) pure public returns(uint){
return a/b;
}
function mod(uint a, uint b) pure public returns(uint){
return a%b;
}
function squ(uint a,uint b) pure public returns(uint){
return a**b;
}
& | ~ ^ << >>
pragma solidity ^0.5.0;
contract math{
// int uint
uint numa = 100;
uint numb = 200;
// uint numc = 2;
function add(uint a, uint b) pure public returns(uint){
return a+b;
}
function sub(uint a, uint b) pure public returns(uint){
return a-b;
}
function mul(uint a, uint b) pure public returns(uint){
return a*b;
}
function div(uint a, uint b) pure public returns(uint){
return a/b;
}
function mod(uint a, uint b) pure public returns(uint){
return a%b;
}
function squ(uint a,uint b) pure public returns(uint){
return a**b;
}
uint8 a = 3;
uint8 b = 4;
function weiyu() pure public returns(uint){
return a&b;
}
function weihuo() pure public returns(uint){
return a|b;
}
function weifan() pure public returns(uint){
return ~a;
}
function weiyihuo() pure public returns(uint){
return a^b;
}
function zuoyi() pure public returns(uint){
return a<<1;
}
function youyi() pure public returns(uint){
return a>>1;
}
}
错误提示:
browser/Math.sol:44:16: TypeError: Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view".
return a&b;
把function weiyu() pure public returns(uint)
修改为:
function weiyu() view public returns(uint)
pragma solidity ^0.5.0;
contract math{
// int uint
uint numa = 100;
uint numb = 200;
// uint numc = 2;
// function add() view public returns(uint){
// return numa + numb;
// }
function add(uint a, uint b) pure public returns(uint){
return a+b;
}
function sub(uint a, uint b) pure public returns(uint){
return a-b;
}
function mul(uint a, uint b) pure public returns(uint){
return a*b;
}
function div(uint a, uint b) pure public returns(uint){
return a/b;
}
function mod(uint a, uint b) pure public returns(uint){
return a%b;
}
function squ(uint a,uint b) pure public returns(uint){
return a**b;
}
uint8 a = 3;
uint8 b = 4;
function weiyu() view public returns(uint){
return a&b;
}
function weihuo() view public returns(uint){
return a|b;
}
function weifan() view public returns(uint){
return ~a;
}
function weiyihuo() view public returns(uint){
return a^b;
}
function zuoyi() view public returns(uint){
return a<<1;
}
function youyi() view public returns(uint){
return a>>1;
}
}
function flow() view public returns(uint){
uint8 mm = 255;
mm++;
return mm; //溢出,返回0
}
function flow2() view public returns(uint){
uint8 mm = 0;
mm--;
return mm; //溢出,返回255
}
function errorTest() public returns(int){
int a = 2;
int b = 0; 不能执行除零
return a/b;
}
***错误提示:***
function flow() view public returns(uint){
uint8 mm = 255;
mm++;
return mm;
}
function flow2() view public returns(uint){
uint8 mm = 0;
mm--;
return mm;
}
function errorTest() public returns(int){
int a = 2;
//int b = 0; 不能执行除零
//return a/b;
int b = -1;
a<
***错误提示:***
function intergerTest() public returns(uint){
uint num = (2**800+1)-2**800;
return num;
}
function intergerTest2() public returns(uint){
// uint num = 2/4.0; 报错,如下所示
uint num = 2/4.0*1000;
return num;
}
function intergerTest3() public returns(uint){
uint num = 111111111111111111111111111111111111111111112- 111111111111111111111111111111111111111111111;
return num;
}
uint num = 2/4.0 错误提示:
browser/Math.sol:93:9: TypeError: Type rational_const 1 / 2 is not implicitly convertible to expected type uint256. Try converting to type ufixed8x1 or use an explicit conversion.
uint num = 2/4.0;
^--------------^
满足精度的需要,可以计算出来只看最后的结果满不满足,赋值给最后的变量
byte代表bytes1
pragma solidity ^0.5.0;
contract ByteArray{
// 0x7a68656e676a69616e78756e
bytes1 public num1 = 0x7a; // 0111 1010 // 加上public后可以自动get到该变量
bytes2 public num2 = 0x7a68; // 0111 1010 0110 1000
bytes12 public num3 = 0x7a68656e676a69616e78756e;
function getLength() public returns(uint){
return num1.length; //output: "uint256: 1"
}
function getLength2() public returns(uint){
return num2.length; //output: "uint256: 2"
}
function getLength3() public returns(uint){
return num3.length; //output: "uint256: 12"
}
}
Length不能被修改
function setLength(){
num1.length=18;
}
错误信息:
browser/ByteArray.sol:22:5: ParserError: Expected pragma, import directive or contract/interface/library definition.
function setLength(){
^------^
pragma solidity ^0.5.0;
contract ByteArray{
// 0x7a68656e676a69616e78756e
bytes1 public num1 = 0x7a; // 0111 1010
bytes2 public num2 = 0x7a68; // 0111 1010 0110 1000
bytes12 public num3 = 0x7a68656e676a69616e78756e;
bytes1 public a = 0x7a; //新增
bytes1 public b = 0x68; //新增
function getLength() public returns(uint){
return num1.length;
}
function getLength2() public returns(uint){
return num2.length;
}
function getLength3() public returns(uint){
return num3.length;
}
// 新增比较操作
function bijiao() public returns(bool){
return a>b;
}
function bijiao2() public returns(bool){
return a>=b;
}
function bijiao3() public returns(bool){
return a>1; // 0x3d
}
}
pragma solidity ^0.5.0;
contract DynamicByte{
bytes public name = new bytes(2); //新建bytes数组,两个空间,public动态生成get方法
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; // 扩充长度在后(右)边加0
}
function pushtest() public{
name.push(0x99); //添加元素在原来数字的后边
}
}