Solidity 從入門到實戰(四)
注意:本專欄主要參考于https://www.bilibili.com/video/BV1St411a7Pk?p=11&spm_id_from=pageDriver的學習筆記以及https://blog.csdn.net/weixin_45067603/article/details/105751748
函式多載
函式多載是指函式命名相同,引數串列不同,即需要滿足以下兩個條件之一
1.函式傳入引數型別不同
2.函式傳入引數數量不同
pragma solidity ^0.4.16;
contract funTest{
uint public test= 0;
function fun1(uint num1,uint num2) {
test = 10;
}
function fun1(uint num1) {
test = 20;
}
function fun2(uint a) {
test = 100;
}
function fun2(string a) {
test = 200;
}
function fun3(address a) {
test=1000;
}
function fun3(uint160 a){
test=2000;
}
function fun4(uint8 a){
test=10000;
}
function fun4(uint16 a){
test=20000;
}
function fun1test() public view returns(uint){
fun1(1,2);
return test;
}
function fun2test() public view returns(uint){
fun2('asdasd');
return test;
}
function fun3test() public view returns(uint){
fun3(0x3e805eC48BdFBc458e7446058F94a315896A1cF6);
//僅使用address型別,可以運行并運行address引數的多載函式
return test;
}
// function fun3test2() public view returns(uint160){
//uint160 如果轉化成uint160型別并運行,那么報錯
//temp=uint160(0x3e805eC48BdFBc458e7446058F94a315896A1cF6);
//fun3(temp);
//return temp;
// }
//**會出現下圖的錯誤**
function fun4test() public view returns(uint){
fun4(256);
return test;
}
function reset() public{
test = 0;
}
}


函式傳入引數
主要有兩種方法進行傳參
1.可以直接對傳入對應引數的值
2.格式 函式名({型別1:value1,型別2:value2,…,型別n:valuen})
3.呼叫函式的時候必須傳入所有引數,否則會報錯
pragma solidity ^0.4.16;
contract funTest{
uint public num;
string public teststring;
function setvalue(uint num1,string teststring1){
num=num1;
teststring = teststring1;
}
function test1(){
setvalue(1,'a');
}
function test2(){
setvalue({num1:1,teststring1:'a'});
}
function test3(){
setvalue({teststring1:'a',num1:1});
}
// function wrongtest4(){
// setvalue(1); 編譯會報錯
//}
}

函式回傳值
1.與go語言類似,solidity可以有多個回傳值,可以命名(型別1 value1,型別2 value2,…,型別n valuen)
2.其它性質都類似于其它語言的特性
pragma solidity ^0.4.16;
contract returnTest{
function test1() public view returns(uint8 num1,uint8 num2,string teststring){
//直接賦值,最后可以看到回傳值就是賦的值
num1=1;
num2=2;
teststring='hello';
}
function test2() public view returns(uint8 num1,uint8 num2,string teststring){
return (10,20,'hello2');//可以直接進行回傳
}
function test3() public view returns(uint8 num1,uint8 num2,string teststring){
num1=1;
num2=2;
teststring='hello';
return (10,20,'hello2');//如果又賦值又回傳,那么以return的值為準
}
function test4() public view returns(uint8 num1,uint8 num2,string teststring1,string teststring2){
teststring1='testnb1';
teststring2='testnb2';
//return (10+20,10*20,'hello2',teststring1+teststring2);//solidity不支持string的直接拼接
return (10+20,10*20,teststring1,teststring2);
}
}

變數的作用域
注意:不要在函式內部重新定義一個和函式形參一樣的變數
pragma solidity ^0.4.16;
contract areaTest{
uint public a=1;
function areatest1(uint a) public view returns(uint){
a=10;
return a;
}
function areatest2(uint a) public view returns(uint){
a=10;
//重新定義a,會出現下圖的錯誤
// for(uint a=0;a<10;a++){
// }
return a;
}
function areatest3() public returns(uint){
//此代碼會改變全域變數a的值
a++;
return a;
}
function areatest4(uint a) public returns(uint){
//此變數只是區域變數,函式執行結束之后就銷毀
a++;
return a;
}
function areatest5() public returns(uint){
//此時重定義a是可行的,覆寫全域變數a的值
uint a;
a=100;
return a;
}
}


constant 關鍵字
1.函式內部的constant關鍵字,在4.x版本中它和view是等價的,在5.0版本中將不能使用
2.全域變數、constant屬性、區域變數是沒有這個屬性的,
3.全域變數加上了constant屬性,就不能被修改,
4.當前版本支持int,uint,string,bytes1–bytes32 能夠使用constant,
pragma solidity ^0.4.16;
contract constantTest{
uint public constant a=1;
//function changetest() public{
// a=2; 函式內試圖修改,結果編譯報錯
//}
// function changetest() public{
// uint constant test=2; //函式內無法宣告constant變數
// }
int public constant b=2;
bytes32 public constant c=0x232334224;
string public constant d='ttttest';
}

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/279315.html
標籤:區塊鏈
