solidity学习记录5

getter函数

pragma solidity ^0.4.0;
contract getter{
//1. public默认的生成get方法  供外部调用

    uint public num = 100;
 
    //2.他等价于下面这个函数  如果编写则默认的会消失
    // function num() external returns(uint){
    //        return num; 
    // }
    //3.默认生成的get函数他是external权限的  不能够在合约的内部调用

    function test(){
        this.num();
    }
       mapping (uint=>string) public map;
       //mapping类型 特殊 默认会生成下面的函数
    //    function map(uint key) returns (string){

    //    }
     function test2(){
        map[2] = "wanshitong";
     }  
     function test3() returns(string) {
         return this.map(2);
     }
//更复杂的映射
 mapping(uint => mapping(uint=>mapping(uint=>string))) public map;
    function test(){
        map[0][1][2]="wanshitong";
        
    }
}

函数继承中的重载

pragma solidity ^0.4.0;

contract father{
    uint money = 10000;
    function dahan() returns(string){
        return "dahan";
    }
}
contract son is father{
    //当子合约与父合约中有相同的属性时子合约中的属性会覆盖掉父合约中的属性
    uint money =100;
    function getMoney() returns(uint){
        return money;
    }
    function dahan() returns(string){
        return "dahansong";
    }
    //子合约会覆盖父合约中的函数
    function test() view returns(string){
        return dahan();
    }
}

合约的多重继承

pragma solidity ^0.4.0;
contract father{
    uint money = 900;
     uint public height=150;
}
contract mother{
   uint public height=180;
}
//多重继承中后面的合约会覆盖前面的合约
contract son is mother,father{

}

 函数的销毁

pragma solidity ^0.4.0;
contract destruct{
    address owner;
    constructor(){
        owner = msg.sender;
    }
    uint public money = 0;
    function increment(){
        money+=10; 
    }
    function kill(){
        if(msg.sender==owner){
            selfdestruct(owner);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40412036/article/details/123256017