Struktury i enummy
Modeluj złożone dane w Solidity za pomocą własnych typów struct i reprezentuj skończone zbiory stanów przy użyciu enumów.
Struktury i enummy to bezpłatna lekcja Blockchain Smart Contracts with Solidity na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Blockchain Smart Contracts with Solidity, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Blockchain Smart Contracts with Solidity zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
Why Custom Types?
Beyond primitives like uint and address, Solidity lets you define your own types. Structs group related fields, and enums represent a fixed set of named states.
Declaring a Struct
A struct bundles several variables under one name, perfect for modeling real-world entities like a user or an order.
struct User {
string name;
uint age;
address wallet;
}Creating Struct Instances
You can construct a struct by positional arguments or by naming each field. Named arguments are clearer and less error prone.
User memory u = User({
name: "Alice",
age: 30,
wallet: msg.sender
});Structs in Storage vs Memory
A struct in storage persists on chain; in memory it is a temporary copy. Assigning a storage struct to a memory variable copies it, so changes do not write back.
Updating Struct Fields
To modify persisted data, work with a storage reference and assign to its fields directly.
User storage u = users[msg.sender];
u.age = 31;Structs Inside Mappings
Structs shine when stored in a mapping keyed by address or id, giving each user their own record.
mapping(address => User) public users;
users[msg.sender] = User("Bob", 25, msg.sender);Declaring an Enum
An enum defines a small set of named options. It improves readability over raw numbers for representing state.
enum Status { Pending, Shipped, Delivered, Cancelled }Using Enum Values
Enum members are stored as uint starting at 0. You compare and assign them by name, which makes contract logic self-documenting.
Status public status = Status.Pending;
function ship() public {
status = Status.Shipped;
}Enums in State Machines
Enums are ideal for modeling a state machine. Guard transitions with require so an order can only move to a valid next state.
function deliver() public {
require(status == Status.Shipped, "Not shipped yet");
status = Status.Delivered;
}Combining Structs and Enums
Put an enum field inside a struct to track each record state, for example storing a Status on every order struct so each order tracks its own lifecycle.
Gas Considerations
Each struct field is a storage slot, and writing storage is expensive. Order fields to pack smaller types together, and use enums (one byte range) instead of strings for fixed states to save gas.
Quick Check
Check your structs and enums knowledge.
Recap
You learned Solidity custom types:
- Structs group related fields and are often stored in mappings
- Mind storage vs memory when copying or updating structs
- Enums model fixed states as integers and power state machines
- Combine them and pack fields to save gas
Custom types make contracts more expressive and maintainable.
Często zadawane pytania
Czy lekcja „Struktury i enummy” jest bezpłatna?
Tak — pełny tekst „Struktury i enummy” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Blockchain Smart Contracts with Solidity, przejdź na CoddyKit PRO. Kurs Blockchain Smart Contracts with Solidity zawiera 4 lekcji w sumie.
Co nauczysz się w „Struktury i enummy”?
Modeluj złożone dane w Solidity za pomocą własnych typów struct i reprezentuj skończone zbiory stanów przy użyciu enumów. Ćwiczysz Blockchain Smart Contracts with Solidity z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Blockchain Smart Contracts with Solidity?
Nie wymagamy żadnego doświadczenia. Blockchain Smart Contracts with Solidity w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Struktury i enummy”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Blockchain Smart Contracts with Solidity?
Tak. Każda lekcja Blockchain Smart Contracts with Solidity zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Typy danych i zmienne w Solidity
- Struktury sterujące i pętle
- Funkcje i modyfikatory widoczności
- Struktury i enummy