--- tags: Week1_Dapp --- # 學習心得 | 技術文章 ``` Dapp_劉力勳_Westbrook ``` [TOC] ## 基礎類別 - Booleans : ``` bool public isActive = true; ``` - Integer : ``` int256 public num = 24; uint256 public num = 24; ``` - Strings : ``` string public str = "Hello World"; ``` - Bytes : ``` bytes1 public h=0x01; ``` - Address : ``` address public owner = 0x7EF2e0048f5bAeDe046f6BF797943daF4ED8CB47 ``` ## 進階類別 - Array : ``` string[] public eggBox = ["egg1", "egg2", "egg3"]; ``` - Mappings : ``` mapping(uunt256 => address) tmp; ``` - Struct : ``` struct Car { uint256 width; string color; string name; } Car public myCar; Car public myCar2; ``` - Enum : ``` enum State { TODO, // 0 COMPLETED, // 1 PAUSED // 2 } //型別 //公開 // Enum bool public isZero = uint256(State.TODO) == 0; // true bool public isOne = uint256(State.COMPLETED) == 1; // false bool public isTwo = uint256(State.PAUSED) == 2; ``` ## Returns ``` function getTrue() public pure returns (bool) { return true; } ``` ``` function getLastReturnValue() public view returns(uint256 z) { (,,z) = multipleReturns(); } ``` ## Visibility ``` 當前合約:部署時最主要的核心合約(Internal) 繼承合約:已經寫好,將功能納入核心合約的合約(Internal) 其他合約:外部與當前合約互動的合約(External) ``` ``` contract Math { function addOne(uint256 x) internal pure returns (uint256) { return x + 1; } } contract Visibility is Math { function coreFunction() public pure returns (string memory) { return "Hello World"; } ``` ## Mutability pure and view ``` function coreFunction() [visibility] [mutability] returns (bool) { return true; } ``` ## Modfier ``` modifier onlyOwner { require(owner == msg.sender); _; } function importantFunction() public onlyOwner view returns (string memory) { return "Hello you are owner!"; } ``` ## Event ``` contract Event { event MyPushLog(uint256 indexed); function debugMode(uint256 _message) public { emit MyPushLog(_message); } } ```