罗韭菜的solidity学习(二)合约中单继承与多继承

子合约继承的方法与属性

  • 只有public的方法会被子合约继承
  • 只有public与internal的属性会被子合约继承
pragma solidity ^0.4.21;

contract Animal {
  uint internal _age;
  uint _weight;
  uint private _height;
  uint public _money;

  function test() constant returns (uint) {
    // this._money
    return _weight;
  }
  
  function test1() constant public returns (uint) {
    return _height;
  }
  function test2() constant internal returns (uint) {
    return _age;
  }
  function test3() constant private returns (uint) {
    return _money;
  }
}
// 只有public的方法会继承到子合约里
contract Dog is Animal{
  function testWeight() constant returns (uint){
    return _weight;
  } 
/*
只有private的属性不会被继承
  function testHeight() constant returns (uint){
    return _height;
  } 

*/
  function testAge() constant returns (uint){
    return _age;
  } 
  function testMoney() constant returns (uint){
    return _money;
  } 
}
只有public方法会被继承

合约里可以多继承

pragma solidity ^0.4.21;

contract Animal {
  uint internal _age;
  uint _weight;
  uint private _height;
  uint public _money;

  function test() constant returns (uint) {
    // this._money
    return _weight;
  }
  
  function test1() constant public returns (uint) {
    return _height;
  }
  function test2() constant internal returns (uint) {
    return _age;
  }
  function test3() constant private returns (uint) {
    return _money;
  }
}

contract Animal1 {
  uint _sex; //1为男,2为女
  funcion Animal1(){
    _sex =1;
  }
  function sex()constant returns (uint){
    return _sex;
  } 
}
// 只有public的方法会继承到子合约里
contract Dog is Animal,Animal1{
  function testWeight() constant returns (uint){
    return _weight;
  } 
/*
只有private的属性不会被继承
  function testHeight() constant returns (uint){
    return _height;
  } 

*/
  function testAge() constant returns (uint){
    return _age;
  } 
  function testMoney() constant returns (uint){
    return _money;
  }
}

你可能感兴趣的:(罗韭菜的solidity学习(二)合约中单继承与多继承)