# Solidity学习之函数可见性 **Published by:** [林夕云](https://paragraph.com/@0xa91ccaeb1206a1fe86c00801555afa1d79fbdd52/) **Published on:** 2022-03-10 **URL:** https://paragraph.com/@0xa91ccaeb1206a1fe86c00801555afa1d79fbdd52/solidity ## Content 重点只有一句话:函数可见修饰符表明了谁可以调用它,private表示只能在当前合约中调用该函数,internal表示只能在当前合约及其子合约中调用该函数,external表示只能在合约外部调用该函数。public表示函数是公开的可以被任意调用。 // SPDX-License-Identifier: MIT pragma solidity ^0.8.10; contract test{ //只能在当前合约中调用 function getNum1() private returns (uint) { return 1; } //只能在当前合约及其子合约中调用 function getNum2() internal returns (uint) { return 1; } //只能被当前合约外部调用 function getNum3() external returns (uint) { return 1; } function getNum4() public returns (uint) { return 1; } function call() public { getNum1(); getNum2(); //compile error,因为external只能被合约外部调用 getNum3(); getNum4(); } } solidity函数可见性无非也和其他语言一样,是为了封装性,你只需要关心合约对外公开的函数功能,那些私有的函数就让他们被开发者折腾吧。 ## Publication Information - [林夕云](https://paragraph.com/@0xa91ccaeb1206a1fe86c00801555afa1d79fbdd52/): Publication homepage - [All Posts](https://paragraph.com/@0xa91ccaeb1206a1fe86c00801555afa1d79fbdd52/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@0xa91ccaeb1206a1fe86c00801555afa1d79fbdd52): Subscribe to updates - [Twitter](https://twitter.com/996icu_ust): Follow on Twitter