18.Constructor
Constructor¶
一个特殊的方法,在合约部署的时候调用,而且只调用这一次
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
// Base contract X
contract X {
address public owner;
uint256 public x;
constructor(uint256 _x) {
owner = msg.sender;
x = _x;
}
}
案例¶
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
// Base contract X
contract Ownable {
address public owner;
constructor() {
owner = msg.sender;
}
// 创建function modifier, 只有owner才能call function
modifier onlyOwner() {
require(msg.sender == owner, "not owner");
_;
}
function setOwner(address _newOwner) external onlyOwner {
require(_newOwner != address(0), "invalid address");
owner = _newOwner;
}
function onlyOwnerCanCallThisFunc() external onlyOwner {
//
}
function anyOneCanCall() external {}
}
案例¶
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
// Base contract X
contract X {
string public name;
constructor(string memory _name) {
name = _name;
}
}
// Base contract Y
contract Y {
string public text;
constructor(string memory _text) {
text = _text;
}
}
// There are 2 ways to initialize parent contract with parameters.
// Pass the parameters here in the inheritance list.
contract B is X("Input to X"), Y("Input to Y") {
}
contract C is X, Y {
// Pass the parameters here in the constructor,
// similar to function modifiers.
constructor(string memory _name, string memory _text) X(_name) Y(_text) {}
}
// Parent constructors are always called in the order of inheritance
// regardless of the order of parent contracts listed in the
// constructor of the child contract.
// Order of constructors called:
// 1. X
// 2. Y
// 3. D
contract D is X, Y {
constructor() X("X was called") Y("Y was called") {}
}
// Order of constructors called:
// 1. X
// 2. Y
// 3. E
contract E is X, Y {
constructor() Y("Y was called") X("X was called") {}
}