📜  Solidity – 枚举和结构

📅  最后修改于: 2022-05-13 01:54:46.686000             🧑  作者: Mango

Solidity – 枚举和结构

枚举是创建用户定义数据类型的方式,它通常用于为整数常量提供名称,使合约更便于维护和阅读。枚举使用几个预定义值之一来限制变量,枚举列表的这些值称为枚举。的选项用从零开始的整数值表示,也可以为枚举指定默认值。通过使用枚举,可以减少代码中的错误。

句法:

enum  { 
            element 1, element 2,....,element n
} 

示例:在下面的示例中,合同类型枚举器 week_days组成, 和函数被定义为设置和获取枚举类型变量的值。

Solidity
// Solidity program to demonstrate
// how to use 'enumerator'
pragma solidity ^0.5.0;
  
// Creating a contract
contract Types { 
 
    // Creating an enumerator
    enum week_days
    {
      Monday,
      Tuesday,
      Wednesday,
      Thursday,
      Friday,
      Saturday,
      Sunday
     } 
 
    // Declaring variables of
    // type enumerator
    week_days week;   
     
    week_days choice;
 
    // Setting a default value
    week_days constant default_value
      = week_days.Sunday;
     
    // Defining a function to
    // set value of choice
    function set_value() public {
      choice = week_days.Thursday;
    }
 
    // Defining a function to
    // return value of choice
    function get_choice(
    ) public view returns (week_days) {
      return choice;
    }
       
    // Defining function to
    //  return default value
    function getdefaultvalue(
    ) public pure returns(week_days) { 
        return default_value; 
    } 
}


Solidity
// Solidity program to demonstrate
// how to use 'structures'
pragma solidity ^0.5.0;
 
// Creating a contract
contract test {
 
   // Declaring a structure
   struct Book {
      string name;
      string writter;
      uint id;
      bool available;
   }
 
   // Declaring a structure object
   Book book1;
 
   // Assigning values to the fields
   // for the structure object book2
   Book book2
     = Book("Building Ethereum DApps",
            "Roberto Infante ",
             2, false);
 
   // Defining a function to set values
   // for the fields for structure book1
   function set_book_detail() public {
      book1 = Book("Introducing Ethereum and Solidity",
                   "Chris Dannen",
                    1, true);
   }
 
 
   // Defining function to print
   // book2 details
   function book_info(
   )public view returns (
     string memory, string memory, uint, bool) { 
           
        return(book2.name, book2.writter,
               book2.id, book2.available); 
    }
    
   // Defining function to print
   // book1 details
   function get_details(
   ) public view returns (string memory, uint) {
      return (book1.name, book1.id);
   }
}


输出 :

枚举

结构

Solidity 允许用户以结构的形式创建自己的数据类型。该结构包含一组具有不同数据类型的元素。一般用它来表示一条记录。使用结构关键字来定义结构,它创建了一个新的数据类型。

句法:

struct  {  
    variable_1;  
    variable_2; 
}

为了访问结构中的任何元素,使用点运算符,它将结构变量和我们希望访问的元素分开。定义结构数据类型的变量使用结构名称。

示例:在下面的示例中,合约 Test一个结构Book 组成,并且定义了函数来设置和获取结构元素的值。

坚固性

// Solidity program to demonstrate
// how to use 'structures'
pragma solidity ^0.5.0;
 
// Creating a contract
contract test {
 
   // Declaring a structure
   struct Book {
      string name;
      string writter;
      uint id;
      bool available;
   }
 
   // Declaring a structure object
   Book book1;
 
   // Assigning values to the fields
   // for the structure object book2
   Book book2
     = Book("Building Ethereum DApps",
            "Roberto Infante ",
             2, false);
 
   // Defining a function to set values
   // for the fields for structure book1
   function set_book_detail() public {
      book1 = Book("Introducing Ethereum and Solidity",
                   "Chris Dannen",
                    1, true);
   }
 
 
   // Defining function to print
   // book2 details
   function book_info(
   )public view returns (
     string memory, string memory, uint, bool) { 
           
        return(book2.name, book2.writter,
               book2.id, book2.available); 
    }
    
   // Defining function to print
   // book1 details
   function get_details(
   ) public view returns (string memory, uint) {
      return (book1.name, book1.id);
   }
}

输出 :

结构