合约中函数的重写

合约中函数的重写

// SPDX-License-Identifier: MIT
pragma solidity ^0.4.0;

contract Person{
  string _name;
  uint internal _age;
  uint private _height;
  uint public _weight;

  function test() constant returns (string) {
    // 读取_name
    return _name;
  }
  function test1() constant public returns (uint) {
    return _height;
  }

  function test2() constant private returns (uint) {
    return _weight;
  }

  function test3() constant internal returns (uint) {
    return _age;
  }
}


contract Person1 {
  string _sex;
  // 构造函数
  function Person1() {
    _sex = "male";
  }

  // get方法
  function sex() constant returns (string) {
    return _sex;
  }
}

contract Bob is Person, Person1 {  // 合约Bob继承自Proson合约

  function testName() constant returns (string) {
    return _name;   
  }

  function testAge() constant returns (uint) {
    return _age;   
  }

  function testWeight() constant returns (uint) {
    return _weight;   
  }

  function sex() constant returns (string) {
    return "female";
  }
}

Bob子合约中sex被重写为"female"。

总结:

  • 如果在Bob子合约中重写sex()函数,会以子函数里面的sex()函数为优先级。

猜你喜欢

转载自blog.csdn.net/qq_44909497/article/details/124217300