# ERC165 解读 > ERC165合约的个人解读 **Published by:** [Confucian](https://paragraph.com/@confucian/) **Published on:** 2022-11-01 **Categories:** smartcontract **URL:** https://paragraph.com/@confucian/erc165 ## Content ERC165解读参考 OpenZeppelin文档 并结合自己的理解什么是ERC165一种用来检测合约所继承接口的标准方法。EIP-165 中提出。为什么要遵守ERC165EIP-165 中的动机:For some "standard interfaces" like the ERC-20 token interface, it is sometimes useful to query whether a contract supports the interface and if yes, which version of the interface, in order to adapt the way in which the contract is to be interacted with. Specifically for ERC-20, a version identifier has already been proposed. This proposal standardizes the concept of interfaces and standardizes the identification (naming) of interfaces.继承了 ERC165 的合约方便他人快速检测其实现了哪些接口。代码实现需实现以下函数:function supportsInterface(interfaceId)IERC165import "@openzeppelin/contracts/utils/introspection/IERC165.sol";// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; interface IERC165 { function supportsInterface(bytes4 interfaceId) external view returns (bool); }如果合约实现了 interfaceId 定义的接口,则返回 trueERC165import "@openzeppelin/contracts/utils/introspection/ERC165.sol";// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "./IERC165.sol"; abstract contract ERC165 is IERC165 { function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { return interfaceId == type(IERC165).interfaceId; } }补充表达式 type(x) 可以检测参数 x 的类型信息。接口类型的特别属性:type(I).interfaceId :返回接口 I 的 bytes4 类型的接口 ID,接口 ID 被定义为 XOR (异或) 接口内所有函数的函数选择器(function selector)。实际使用ERC721合约中对ERC165的重写:function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) { return interfaceId == type(IERC721).interfaceId || interfaceId == type(IERC721Metadata).interfaceId || super.supportsInterface(interfaceId); } ## Publication Information - [Confucian](https://paragraph.com/@confucian/): Publication homepage - [All Posts](https://paragraph.com/@confucian/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@confucian): Subscribe to updates - [Twitter](https://twitter.com/Confucian_e): Follow on Twitter ## Optional - [Collect as NFT](https://paragraph.com/@confucian/erc165): Support the author by collecting this post - [View Collectors](https://paragraph.com/@confucian/erc165/collectors): See who has collected this post