GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Solidity/18_Import at main · flexycode/Solidity

Expand file tree

/

18_Import

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
 
 
/

18_Import

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
img
img
 
 
Import.sol
Import.sol
 
 
Yeye.sol
Yeye.sol
 
 
readme.md
readme.md
 
 

readme.md

Outline
title 18. Import
tags
solidity
advanced
wtfacademy
import

WTF Solidity极简入门: 18. Import

我最近在重新学 Solidity,巩固一下细节,也写一个“WTF Solidity极简入门”,供小白们使用(编程大佬可以另找教程),每周更新 1-3 讲。

推特:@0xAA_Science|@WTFAcademy_

社区:Discord|微信群|官网 wtf.academy

所有代码和教程开源在 github: github.com/AmazingAng/WTF-Solidity


在Solidity中,import语句可以帮助我们在一个文件中引用另一个文件的内容,提高代码的可重用性和组织性。本教程将向你介绍如何在Solidity中使用import语句。

import用法

  • 通过源文件相对位置导入,例子:

    文件结构
    ├── Import.sol
    └── Yeye.sol
    
    // 通过文件相对位置import
    import './Yeye.sol';
    
  • 通过源文件网址导入网上的合约的全局符号,例子:

    // 通过网址引用
    import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol';
    
  • 通过npm的目录导入,例子:

    import '@openzeppelin/contracts/access/Ownable.sol';
  • 通过指定全局符号导入合约特定的全局符号,例子:

    import {Yeye} from './Yeye.sol';
  • 引用(import)在代码中的位置为:在声明版本号之后,在其余代码之前。

测试导入结果

我们可以用下面这段代码测试是否成功导入了外部源代码:

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

// 通过文件相对位置import
import './Yeye.sol';
// 通过`全局符号`导入特定的合约
import {Yeye} from './Yeye.sol';
// 通过网址引用
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol';
// 引用OpenZeppelin合约
import '@openzeppelin/contracts/access/Ownable.sol';

contract Import {
    // 成功导入Address库
    using Address for address;
    // 声明yeye变量
    Yeye yeye = new Yeye();

    // 测试是否能调用yeye的函数
    function test() external{
        yeye.hip();
    }
}

result

总结

这一讲,我们介绍了利用import关键字导入外部源代码的方法。通过import关键字,可以引用我们写的其他文件中的合约或者函数,也可以直接导入别人写好的代码,非常方便。

You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.