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类型的函数,不能继承internal/private的函数

private 
    1.private私有类型,只能在合约内部使用,所以到我们在子合约中尝试使用时,就会报错。

重写
    子合约可以将父合约的public类型的函数,只能继承public类型的函数,继承过来的函数进行重写

猜你喜欢

转载自www.cnblogs.com/eilinge/p/9951888.html