contract Base {
// [Scope]
// - inside Base Contract
// - inside contracts that inherit this contract
// - by other contracts
string public publicMsg = "public state variables"; function testScope() public virtual {
// ✔️ 可以存取「Base」的public 變數
require(bytes(publicMsg).length > 0, "cannot access public state variable");
}} contract Extend is Base { function testScope() public view override {
// ✔️ 可以存取「Base」的public 變數
require(bytes(publicMsg).length > 0, "cannot access public state variable");
}
}contract External {
function testScope() public {
// 宣告Base合約並呼叫其開放外部的Job
Base b = new Base(); // ✔️ 可以存取「Base」的public變數
b.publicMsg;
}
}
internal
除了自身的合約以外,繼承的合約也能夠調用,但外部合約是不可視的。
contract Base {
// [Scope]
// - inside Base Contract
// - inside contracts that inherit this contract
string internal internalMsg = "internal state variables"; function testScope() public virtual {
// ✔️ 可以存取「Base」的internal 變數
require(bytes(internalMsg).length > 0, "cannot access internal state variable");
}} contract Extend is Base { function testScope() public view override {
// ✔️ 可以存取「Base」的internal 變數
require(bytes(internalMsg).length > 0, "cannot access internal state variable");
}
}contract External {
function testScope() public {
// 宣告Base合約並呼叫其開放外部的Job
Base b = new Base();
// ❌ 無法存取「Base」的internal變數
// @notice 語法檢測錯誤: not found or not visible after argument-dependent lookup in contract Base.
// b.internalMsg;
}
}
private
這種可視範圍僅侷限於自身的合約之中,不可被外部合約調用,包括繼承的合約也是不可視的。
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7.0;contract Base {
// State variables
// [Scope]
// - inside Base Contract
string private privateMsg = "private state variables";
function testScope() public virtual {
// ✔️ 可以存取「Base」的private 變數
require(bytes(privateMsg).length > 0, "cannot access private state variable");
}} contract Extend is Base {
function testScope() public view override {
// ❌ 無法使用基礎合約的私有狀態變數
// @notice 語法檢測錯誤: DeclarationError: Undeclared identifier.
// require(bytes(privateMsg).length > 0, "cannot access private state variable");
}
}contract External {
function testScope() public {
// 宣告Base合約並呼叫其開放外部的Job
Base b = new Base(); // ❌ 無法存取「Base」的private變數
// @notice 語法檢測錯誤: not found or not visible after argument-dependent lookup in contract Base.
// b.privateMsg;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7.0;contract Base {
// [Scope]
// - by other contracts
function externalJob() external pure returns (string memory) {
return "external job called";
} function testScope() public virtual {
// ❌ 僅能由外部合約進行呼叫
// @notice 語法檢測錯誤: DeclarationError: Undeclared identifier. Did you mean "internalJob" or "externalJob"?
// externalJob(); // ✔️ 但可以使用這種方式在內部進行調用
this.externalJob();
}} contract External {
function testScope() public {
// 宣告Base合約並呼叫其開放外部的Job
Base b = new Base();
b.externalJob();
}
}
public
可視範圍就如同狀態變數的public一樣,除了內部合約以外,繼承與外部合約也都能夠進行調用。
contract Base {
// [Scope]
// - inside this contract
// - inside contracts that inherit this contract
// - by other contracts
function publicJob() public pure returns (string memory) {
return "public job called";
} function testScope() public virtual {
// ✔️ 可以存取「Base」的public Function
publicJob();
}} contract Extend is Base { function testScope() public view override {
// ✔️ 可以存取「Base」的public Function
publicJob();
}
}contract External {
function testScope() public {
// 宣告Base合約並呼叫其開放外部的Job
Base b = new Base(); // ✔️ 可以存取「Base」的public Function
b.publicJob();
}
}
contract Base {
// [Scope]
// - inside this contract
// - inside contracts that inherit this contract
function internalJob() internal pure returns (string memory) {
return "internal job called";
} function testScope() public virtual {
// ✔️ 可以存取「Base」的internal Function
internalJob();
}} contract Extend is Base { function testScope() public view override {
// ✔️ 可以存取「Base」的internal Function
internalJob();
}
}contract External {
function testScope() public {
// 宣告Base合約並呼叫其開放外部的Job
Base b = new Base(); // ❌ 無法存取「Base」的internal Function
// @notice 語法檢測錯誤: not found or not visible after argument-dependent lookup in contract Base.
// b.internalJob();
}
}
private
這種可視範圍僅侷限於自身的合約之中,不可被外部合約調用,包括繼承的合約也是不可視的。
contract Base {
// [Scope]
// - inside Base Contract
function privateJob() private pure returns(string memory) {
return "private job called";
} function testScope() public virtual {
// ✔️ 可以存取「Base」的private Function
privateJob();
}} contract Extend is Base { function testScope() public view override {
// ❌ 無法使用基礎合約的私有成員Function
// @notice 語法檢測錯誤: DeclarationError: Undeclared identifier
// privateJob();
}
}contract External {
function testScope() public {
// 宣告Base合約並呼叫其開放外部的Job
Base b = new Base(); // ❌ 無法存取「Base」的private Function
// @notice 語法檢測錯誤: not found or not visible after argument-dependent lookup in contract Base.
// b.privateJob();
}