Structs und Enums
Modellieren Sie komplexe Daten in Solidity mit benutzerdefinierten Struct-Typen und stellen Sie feste Zustandsmengen mit Enums dar
Structs und Enums ist eine kostenlose Blockchain Smart Contracts with Solidity-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Blockchain Smart Contracts with Solidity-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.
Häufig gestellte Fragen
Ist die Lektion „Structs und Enums“ kostenlos?
Ja — der vollständige Text von „Structs und Enums“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Blockchain Smart Contracts with Solidity-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Blockchain Smart Contracts with Solidity-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Structs und Enums“?
Modellieren Sie komplexe Daten in Solidity mit benutzerdefinierten Struct-Typen und stellen Sie feste Zustandsmengen mit Enums dar Du übst Blockchain Smart Contracts with Solidity mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Blockchain Smart Contracts with Solidity zu starten?
Keine Vorkenntnisse erforderlich. Blockchain Smart Contracts with Solidity auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „Structs und Enums“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Blockchain Smart Contracts with Solidity-Lektion Code schreiben und ausführen?
Ja. Jede Blockchain Smart Contracts with Solidity-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Solidity-Datentypen und Variablen
- Kontrollstrukturen und Schleifen
- Funktionen und Sichtbarkeitsmodifikatoren
- Structs und Enums