solidity学习记录5

本文介绍了Solidity智能合约中的getter函数、继承、多重继承和函数销毁的概念。通过示例展示了如何使用getter函数访问合约变量,如何进行函数重载,以及如何在多重继承中处理属性覆盖。此外,还探讨了如何在合约中实现自我销毁功能。
摘要由CSDN通过智能技术生成

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);
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值