Solidity合约继承

Solidity合约继承

pragma solidity ^0.4.4;


//public internal private
contract Animal {
  uint _weight;
  uint private _hegiht;
  uint internal _age;
  uint public _money;

  function test() constant returns(uint) {
    return _weight;
  }
  
  function test1() constant public returns(uint) {
    return _hegiht;
  }
  function test2() constant internal returns(uint) {
    return _age;
  }
  function test3() constant private returns(uint) {
    return _money;
  }
  
  function testInternal() constant returns(uint){
      return this.test();
  }
   function testInternal2() constant returns(uint){
      return this.test1();
  }
   function testInternal3() constant returns(uint){
      return test2();//internalhe和private 直接访问就行
  }
   function testInternal4() constant returns(uint){
      return test3();
  }

}
contract Dog is Animal{
 
    function testWeight() constant returns(uint){
        return _weight;
    }
    //唯独不能继承private类型
    /*function testHeight() constant returns(uint){
        return _hegiht;
    }*/
    
    function testAge() constant returns(uint){
        return _age;
    }
    function testMoney() constant returns(uint){
        return _money;
    }
}

Solidity合约继承_第1张图片
总结:子合约只能继承public类型的函数,而子合约可以继承public和internal类型的属性


  • 多继承:
pragma solidity ^0.4.4;


//public internal private
contract Animal {
  uint _weight;
  uint private _hegiht;
  uint internal _age;
  uint public _money;

  function test() constant returns(uint) {
    return _weight;
  }
  
  function test1() constant public returns(uint) {
    return _hegiht;
  }
  function test2() constant internal returns(uint) {
    return _age;
  }
  function test3() constant private returns(uint) {
    return _money;
  }
  
  function testInternal() constant returns(uint){
      return this.test();
  }
   function testInternal2() constant returns(uint){
      return this.test1();
  }
   function testInternal3() constant returns(uint){
      return test2();//internalhe和private 直接访问就行
  }
   function testInternal4() constant returns(uint){
      return test3();
  }

}
contract Animal1{
    uint _sex; //1,0:man,wamen
    function Animal(){
        _sex=1;
    }
    function sex()constant returns(uint){
        return _sex;
    }
}
contract Dog is Animal,Animal1{
 
    function testWeight() constant returns(uint){
        return _weight;
    }
    // function testHeight() constant returns(uint){
    //     return _hegiht;
    // }
    function testAge() constant returns(uint){
        return _age;
    }
    function testMoney() constant returns(uint){
        return _money;
    }
}

Solidity合约继承_第2张图片

你可能感兴趣的:(Solidity)