# Solidity的方方面面

Solidity的方方面面

### 什么是Soilidity?

Solidity是一种面向合约的高级编程语言，用于实现智能合约。Solidity已经被设计用于以太坊虚拟机。

Solidity===智能合约。

Solidity的代码封装在合约中。合约是以太坊应用程序的基本构建块——所有变量和函数都属于一个合约，这将是所有项目的起点。

``````pragma solidity >=0.5.0 &lt;0.6.0;

contract HelloWorld {

}``````

### 状态变量和整数

``````contract Example {

// This will be stored permanently in the blockchain

uint myUnsignedInteger = 100;

string name = "vivek"

}``````

Uint数据类型是一个无符号整数。它应该是非负的。

### 数据类型

#### 值类型：

`Boolean(true / false)，Integers(int / uint)， Address(以太坊地址的大小)，String, enum`

#### 数学运算

``````Addition: x + y

Subtraction: x — y,

Multiplication: x * y

Division: x / y

Modulus / remainder: x % y (for example, 13 % 5 is 3, because if you divide 5 into 13, 3 is the remainder)

Exponential Operation uint x = 5 ** 2; // equal to 5² = 25``````

### 结构

``````struct Person {

uint age;

string name;``````

### 数组

``````// Array with a fixed length of 2 elements:

uint[2] fixedArray;

// another fixed Array, can contain 5

strings: string[5] stringArray;

// a dynamic Array — has no fixed size, can keep growing:

uint[] dynamicArray;``````

``````pragma solidity >=0.5.0 &lt;0.6.0;

contract StudentFactory {

struct student {``````
``````string name;

uint roll; } student[] public students; // creates an array named students of student type objects``````

### 函数声明

`function eatHamburgers(string memory _name, uint _amount) public { }`

`eatHamburgers(“vitalik”, 100);`

### 私人/公共函数

``````function _eatHamburgers(string memory _name, uint _amount) private {

}``````

### 在函数中返回

``````function sayHi() public view/pure returns (string memory) {

return “Hi”;

}``````

### 类型转换

``````uint8 a = 5;

uint b = 6;

// line below throws an error because a*b returns a uint, not uint8:

uint8 c = a * b;

// we have to typecast b as a uint8 to make it work:

uint8 c = a * uint8(b);``````

### 事件

``````// declare the event

event NotifyOnFrontend(uint x);

function add(uint _x, uint _y) public returns (uint) {

uint result = _x + _y;

//fire an event to let the frontend know the function was called

emit NotifyOnFrontend(result);

return result;

}``````

``````YourContract.NotifyOnFrontend(function(error, result) {

// do something with result

})``````

### 映射

`mapping (address => uint) public accountBalance;`

contract Example {

``````struct UserInfo {

unit age; string dob;
}``````
``mapping(string => UserInfo) allusers;``
``````function setUserInfo(string _name, uint _age, string _dob) public {
allusers[_name].age = _age;
allusers[_name].dob = _dob;
}``````
``````function getUserInfo(string *name) public view returns(uint, string) {
return (allusers[* name].age, allusers[_name].dob);
}

}``````

`setuserInfo("Vivek",26, 25/05/1995) setuserInfo("Supu", 23, 01/09/1998)`

``````getUserInfo("Vivek"); // 26 25/05/1995

getuserInfo("Supu"); // 24 01/09/1998``````

### require

require用于验证这两个语句，并据此做出决定。如果条件为真，则代码成功运行，否则就抛出错误

``````function sayHi(string memory *name) public returns (string memory) { /**Compares if _name equals “Vivek” Throws an error and exits if not true. Solidity doesn’t have native string comparison, so we compare their keccak256 hashes to see if the strings are equaq ** / *

*require(keccak256(abi.encodePacked(* name)) == keccak256(abi.encodePacked(“Vivek”)));

// If it’s true, proceed with the function:

return “Hi!”;

}

sayHi(“Vivek”) // executes successfully

sayHi(“Supu”) // throws an error``````

### 继承

``````contract Animal {

function catchphrase() public returns (string memory) {
return “Animal”;
}

} contract Cat is Animal {

function anotherCatchphrase() public returns (string memory) {
return “Cat is an Animal”;
}

}``````

### 与区块链网络中的其他合约交互

``````contract GetNumber {

function getNum(uint _num) public returns(uint){
return _num;
}``````

``````contract NumberInterface {

function getNum(uint _num) public returns(uint);

}``````

### For循环

Syntax类似于Javascript。

`for (uint i = 1; i &lt;= 10; i++) { // body }`

### assert 与 require 的差异

Assert类似于require，如果为false则抛出错误。assert和require之间的区别是，当一个函数失败时，require会退还用户剩余的gas，而assert不会。