• ethereum(以太坊)(三)--合约单继承与多继承


    pragma solidity ^0.4.0;
    
    // priveta public internal
    
    contract Test{
        //defualt internal
        uint8 internal _money;
        uint8 _age;
        uint8 private _height;
        uint8 public _sex;
        
        function test() constant returns(uint8){
            return _money;
        }
        
        function testAge() constant returns(uint8){
            return _age;
        }
        
        function testHeight()constant returns(uint8){
            return _height;
        }
        
        function testSex() constant returns(uint8){
            return _sex;
        }
        
        function testSex1() external returns(uint8){
            return 10;
        }
        
        function testAddress1() constant returns(address){
            return msg.sender; //0xca35b7d915458ef540ade6068dfe2f44e8fa733c
        }
    
    }
    
    contract Test1{
        uint _mouth;
        function Test1(){
            _mouth = 11;
        }
        function mouth() returns(uint){
            return _mouth;
        }
    }
    
    contract SubTest is Test,Test1{
        
        function testAddress2()constant returns(address){
            return msg.sender;//0xca35b7d915458ef540ade6068dfe2f44e8fa733c
        }
        function testInternel1()constant returns(uint8){
            return _money;
        }
        function testInternel2()constant returns(uint8){
            return _age;
        }
        /*DeclarationError: Undeclared identifier
        function testPrivate() returns(uint8){ //cannot call Private function/attribute
            return _height;
        }
        */
        function testPublic()constant returns(uint8){
            return _sex;
        }
        function testExternal()constant returns(uint8){
            return this.testSex1();
            //return this.testSex1; TypeError: Return argument type function () external returns (uint8) 
            //is not implicitly convertible to expected type (type of first return variable) uint8
            //return testSex1; DeclarationError: Undeclared identifier
        }
        
        function mouth() returns(uint){
            return 100; //Rewriting the Mouth function in the subcontract overrides the value written by the parent function,only Public function can be overrided
        }
    }
    public
        1.public类型的状态变量和函数的权限最大,可供外部、子合约、合约内部访问。
        2.状态变量声明时,默认为internal类型,只有显示声明为public类型的状态变量才会自动生成一个和状态变量同名的get函数以供外部获取当前状态变量的值。
        函数声明时默认为public类型,和显示声明为public类型的函数一样,都可供外部访问。
    
    internal
        1.internal类型的状态变量可供外部和子合约调用。
        2.子合约中能继承父合约中的所有的public/external类型的函数,不能继承private的函数,也无法调用;internal无法继承,可以调用
    private 1.private私有类型,只能在本合约内部使用,所以在子合约中尝试使用时,就会报错。 
    external
      1.子合约调用父合约中的external函数时,可以用this.
    testSex1()/testSex1()
    重写
        子合约可以将父合约的public类型的函数,只能继承public类型的函数,继承过来的函数进行重写(父函数不是只读属性(constant))
       //https://pan.baidu.com/s/1cRICAY9aSHn6vNI4YN1tQQ
  • 相关阅读:
    int func(int aa[4]) { return sizeof(aa);}
    C语言中的undefined behavior系列(3) trap representation
    面试被轮及访博客园三人组蹭饭记 20100910
    关于带新人和作新人。
    [C++]Call virtual member function in constructor or destructor
    The using declaration in C++
    WF & WCF(5)
    sqlserver 常用函數
    WF & WCF(4)
    oracle 三種集合類型比較(indexby表,嵌套表,數組)
  • 原文地址:https://www.cnblogs.com/eilinge/p/9951888.html
Copyright © 2020-2023  润新知