1、string
pragma solidity ^0.4.0;
contract StringTest{
string name = "xms";
function getName() public view returns(string){
return name;
}
function setName(string _name) public{
name = _name;
}
function pureTest(string _name) pure public returns(string){
return _name;
}
}
2、bool
pragma solidity ^0.4.0;
contract booltest{
bool _a;
int num1 = 100;
int num2 = 200;
function getBool() returns(bool){
return _a;//false
}
function getBool2() returns(bool){
return !_a;//true
}
function panduan() returns(bool){
return num1 == num2;
}
// && ||
}
3、int
pragma solidity ^0.4.0;
contract inttest{
int num = 100; //int256
uint unum = 200; //uint256
uint8 num8 = 255; //8位 不能超过256
function add(uint a, uint b)pure public returns(uint){
return a+b;
//+ - * / % **(平方)
//整形直面量 uint8 num = 2/4.0*1000;
}
}
4、位运算
pragma solidity ^0.4.0;
contract bittest{
//& | ~ ^ << >>
uint8 a = 3;
uint8 b = 4;
function weiyu() view public returns(uint8){
return a&b;
}
}
5、数组
pragma solidity ^0.4.0;
contract arrtest{
//bytes1 bytes2 bytes3 *******bytes32
bytes1 num1 = 0x7a;
bytes2 num2 = 0x7a68; //bytes2 public num2 加public自动生成get方法
function getlen() returns(uint){
return num1.length;
}
bytes public name = new bytes(2);
function setName(){
name[0] = 0x71;
name[1] = 0x68;
}
function getlenth() view returns(uint){
return name.length;
}
function changelenth(){
name.length = 5;
}
function pushtest(){
name.push(0x99);
}
}
pragma solidity ^0.4.0;
contract StringArrayTest{
string name = "zhengjianxun";//中文占3个字节
function getlen() view returns(uint){
//return name.length;
return bytes(name).length;
}
function getName() view returns(bytes){
return bytes(name);
}
function changeName(){
//return name[0];
bytes(name)[1] = 'L';
}
}
pragma solidity ^0.4.0;
contract transferbytes{
bytes12 name = 0x7a68656e676a69616e78756e;
function changeByte1() view returns(bytes1){
return bytes1(name);
}
function fixname() view returns(bytes){
bytes memory newname = new bytes(name.length);
for(uint i = 0;i
6、bytes转string
pragma solidity ^0.4.0;
contract bytestostring{
bytes name = new bytes(2);
function init(){
name[0] = 0x7a;
name[1] = 0x68;
}
function bytestostring1() view returns(string){
return string(name);
}
}
pragma solidity ^0.4.0;
contract bytestostring2{
bytes2 name = 0x7a68;
function bytestostringtest1(bytes32 _newname) view returns(string){
bytes memory newname = new bytes(_newname.length);
for(uint i = 0;i<_newname.length;i++){
newname[i] = _newname[i];
}
return string(newname);
}
function bytestostringtest2(bytes32 _newname) view returns(string){
uint count = 0;
for(uint i = 0;i<_newname.length;i++){
if(_newname[i]!=0){
count++;
}
}
bytes memory newname = new bytes(count);
for(uint j = 0;j
7、固定uint数组
pragma solidity ^0.4.0;
contract FixArray{
uint[5] arr = [1,2,3,4,5];
function init(){
arr[0] = 100;
arr[1] = 200;
}
function getarr() public view returns(uint[5]){
return arr;
}
function getsum() public view returns(uint){
uint sum = 0;
for(uint i = 0;i
8、固定二维数组
pragma solidity ^0.4.0;
contract twoarray{
uint[2][3] grade = [[1,2],[3,4],[5,6]];//初始化不一致,访问一致
function getlen() returns(uint){
return grade.length;
}
function genlen2() returns(uint){
return grade[0].length;
}
function getcontent() view returns(uint[2][3]){
return grade;
}
function add() view returns(uint){
uint sum = 0;
for(uint i = 0;i
9、mapping
pragma solidity ^0.4.0;
contract mappingtest{
mapping(address => uint) idmapping;
mapping(uint => string) namemapping;
uint id = 0;
function register(string name){
address account = msg.sender;
id++;
idmapping[account] = id;
namemapping[id] = name;
}
function getidbyaddress(address are) returns(string){
return namemapping[idmapping[are]];
}
}
10、函数
函数重载:方法名一致,参数个数以及参数类型不一致,返回值不一样不能说明重载,记住uint160和address类型是一致的。
函数命名参数:可以通过{name:"saadf",id:10} 这样设置
函数返回值:可以命名,多返回值,进行交换值
11、构造函数
pragma solidity ^0.4.0;
contract gouzao{
uint public a;
function gouzao(){
a=100;
}
//新版本
constructor(){
a=100;
}
}
//部署合约后就进行执行该方法。构造函数只能有一个,可以加多个参数。
12、modifier函数
pragma solidity ^0.4.0;
contract modifiertest{
address public owner;
uint public num = 0;
constructor(){
owner = msg.sender;
}
modifier OnlyOwner{
require(msg.sender == owner);
_;//动态添加
}
//附加上了modifier,先执行了require,判断如果整理再执行num,如果不成立回滚
function changeid(uint _num) OnlyOwner{
num = _num;
}
}
13、多重modifier函数
pragma solidity ^0.4.0;
contract modifiermul{
uint public a = 0;
modifier mod1{
a = 1;
_;
a = 2;
}
modifier mod2{
a = 100;
_;
a = 200;
}
// 先1 100 100 200 2
function test() mod1 mod2{
a = 100;
}
}
14、合约销毁
pragma solidity ^0.4.0;
contract destruct{
address owner;
constructor(){
owner = msg.sender;
}
function kill(){
if(msg.sender == owner){
selfdestruct(owner);
}
}
}