# 【合約互動】

我們可以使用 `address()` 將 `contract` 型態轉為 `address` 型態。

如果今天要使用同一個檔案裏面其他合約的函數，可以使用以下方法：

```solidity
contract A {
  function foo() view external returns(uint) {...}
}

contract B {
  function callFoo(address addrA) external {
    uint result = A(addrA).foo();
  }
}
```

甚至可以在一個合約裡面宣告其他合約。

```solidity
contract A {
  constructor(uint a) {...}
  function foo() external {...}
}

contract B {
  function createA(uint a) external {
    A AInstance = new A(a); //pass constructor argument(s) if any
  }
}
```

#### Modifier, Inheritance, Importing

```solidity
pragma solidity ^0.8.11;

contract Owned {
    address owner;
    constructor() public {
        owner = msg.sender;
    }
    
    modifier onlyOwner() {
        require(msg.sender == owner, "You are not allowed");
        _;
    }    
}

contract InheritanceModifierExample is Owned {

    mapping(address => uint) public tokenBalance;


    uint tokenPrice = 1 ether;

    constructor() public {
        tokenBalance[owner] = 100;
    }

    function createNewToken() public onlyOwner{
        // require statement is originally here
        tokenBalance[owner]++;
    }

    function burnToken() public onlyOwner{
        // require statement is originally here
        tokenBalance[owner]--;
    }

    function purchaseToken() public payable {
        require((tokenBalance[owner] * tokenPrice) / msg.value > 0, "not enoug
        h tokens");
        tokenBalance[owner] -= msg.value / tokenPrice;
        tokenBalance[msg.sender] += msg.value / tokenPrice;
    }

    function sendToken(address _to, uint _amount) public {
        require(tokenBalance[msg.sender] >= _amount, "Not enough tokens");
        assert(tokenBalance[_to] + _amount >= tokenBalance[_to]);
        assert(tokenBalance[msg.sender] - _amount <= tokenBalance[msg.sender])
        ;
        tokenBalance[msg.sender] -= _amount;
        tokenBalance[_to] += _amount;
    }
}
```

Or we can clip the code to two parts(solidity file, Owned.sol and Modifier.sol)

* Owned.sol

```solidity

contract Owned {
    address owner;
    constructor() public {
        owner = msg.sender;
    }
    
    modifier onlyOwner() {
        require(msg.sender == owner, "You are not allowed");
        _;
    }    
}
```

* Modifier.sol

```solidity
pragma solidity ^0.8.11;

import "./Owned.sol"

contract InheritanceModifierExample is Owned {

    mapping(address => uint) public tokenBalance;

    uint tokenPrice = 1 ether;

    constructor() public {
        tokenBalance[owner] = 100;
    }

    function createNewToken() public onlyOwner{
        // require statement is originally here
        tokenBalance[owner]++;
    }

    function burnToken() public onlyOwner{
        // require statement is originally here
        tokenBalance[owner]--;
    }

    function purchaseToken() public payable {
        require((tokenBalance[owner] * tokenPrice) / msg.value > 0, "not enoug
        h tokens");
        tokenBalance[owner] -= msg.value / tokenPrice;
        tokenBalance[msg.sender] += msg.value / tokenPrice;
    }

    function sendToken(address _to, uint _amount) public {
        require(tokenBalance[msg.sender] >= _amount, "Not enough tokens");
        assert(tokenBalance[_to] + _amount >= tokenBalance[_to]);
        assert(tokenBalance[msg.sender] - _amount <= tokenBalance[msg.sender])
        ;
        tokenBalance[msg.sender] -= _amount;
        tokenBalance[_to] += _amount;
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://chihaolu.gitbook.io/all-in-one-solidity/part-ii-medium/chapter-11-ji-cheng-inheritance/he-yue-hu-dong.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
