[Solidity] Interface

yooni·2022년 3월 28일
1
post-thumbnail

Interface


You can interact with other contracts by declaring an Interface.
인터페이스는 다른 컨트랙트와 상호작용하기 위해 사용된다.

자식 컨트랙트를 위한 틀이며, 추상 함수로만 구성되어야 한다. 해당 내용을 구현하지는 않고 이를 상속하는 쪽에서 구현하게 하되, 다른 컨트랙트들에게 이 컨트랙트는 이런 함수들을 포함하니 안심하고 호출하라는 정보를 주는 역할을 한다.

추상 함수
함수의 이름, 입력 매개변수, 출력만 선언해두고 내용은 없는 함수


interface 특징

  • cannot have any functions implemented
    함수의 기능은 정의하지 않는다.

  • can inherit from other interfaces
    다른 인터페이스에서 상속 가능하다.

  • all declared functions must be external
    함수는 무조건 external 타입이어야 한다.

  • cannot declare a constructor
    생성자를 선언할 수 없다.

  • cannot declare state variables
    상태 변수를 선언할 수 없다.


example

pragma solidity ^0.5.0;

interface ICalculator {
  function getResult() external view returns (uint);
}

contract Test is ICalculator {
  constructor() public {}
  function getResult() external view returns (uint) {
    uint a = 1;
    uint b = 2;
    uint result = a + b;
    return result;
  }
}



📌 Reference
https://que-n-a.tistory.com/entry/solidity-%EA%B8%B0%EB%B3%B8-%EB%AC%B8%EB%B2%95-5-%EC%83%81%EC%86%8D-Interface-%EB%8B%A4%EC%88%98-return-%EA%B0%92-%EC%B2%98%EB%A6%AC-with-%ED%81%AC%EB%A6%BD%ED%86%A0%EC%A2%80%EB%B9%84
https://goodgid.github.io/Ethereum-Basic-Solidity-(8)/
https://solidity-by-example.org/interface/
https://www.tutorialspoint.com/solidity/solidity_interfaces.htm
https://steemit.com/ethereum/@kblock/69-zeppelin-solidity-smartcontract-2

profile
멋쟁이 코린이

2개의 댓글

comment-user-thumbnail
2022년 10월 16일

잘지내시죠? ㅎㅎ

1개의 답글