Methoden und der self-Parameter
Verknüpfen Sie Verhalten mit Ihren Structs.
Methoden und der self-Parameter ist eine kostenlose Zig Academy-Lektion auf CoddyKit. Dies ist Lektion 2 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 Zig Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Data Plus Behavior
A struct can hold more than fields. You can declare methods inside it: functions that act on the data they belong to.
Functions Live Inside
Write a normal fn inside the struct body. It becomes namespaced under the type, callable as Type.name.
const Point = struct {
x: i32,
y: i32,
fn origin() Point {
return Point{ .x = 0, .y = 0 };
}
};The self Parameter
A method that works on an instance takes the value as its first parameter, named self by convention.
fn magnitude(self: Point) i32 {
return self.x + self.y;
}Call With a Dot
Call a method through an instance with a dot. Zig passes the instance as self automatically for you.
const p = Point{ .x = 3, .y = 4 };
const m = p.magnitude();self Is Just an Argument
There is no hidden magic. The dot call p.magnitude() is exactly the same as Point.magnitude(p).
const m = Point.magnitude(p);Reading self by Value
Taking self by value gives you a copy. That is fine for reading fields, but changes to it will not stick.
fn sum(self: Point) i32 {
return self.x + self.y;
}Mutating Needs a Pointer
To change fields, take self as a pointer with *Point. Now writes go to the original instance, not a copy.
fn moveRight(self: *Point) void {
self.x += 1;
}Auto-Reference on Call
When a method wants *Point, Zig takes the address of your var for you. You still just write p.moveRight().
var p = Point{ .x = 0, .y = 0 };
p.moveRight();Use @This for the Type
Inside the body you can refer to the enclosing type with @This(), handy when the type has no outer name yet.
const Counter = struct {
n: i32,
fn get(self: @This()) i32 {
return self.n;
}
};Methods Without self
A method need not take self at all. Constructors and helpers often skip it and act as plain namespaced functions.
fn init(x: i32, y: i32) Point {
return Point{ .x = x, .y = y };
}Organizing With Methods
Grouping behavior next to data keeps your code tidy. Methods make a struct read like a small, self-contained component.
Quick Check
What does a method need to change an instance's fields?
Recap
You added methods: functions inside a struct, with self as the instance. Use self: Point to read and self: *Point to mutate. 🔧
Häufig gestellte Fragen
Ist die Lektion „Methoden und der self-Parameter“ kostenlos?
Ja — der vollständige Text von „Methoden und der self-Parameter“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Zig Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Zig Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Methoden und der self-Parameter“?
Verknüpfen Sie Verhalten mit Ihren Structs. Du übst Zig Academy 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 Zig Academy zu starten?
Keine Vorkenntnisse erforderlich. Zig Academy 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 2 von 4.
Wie lange dauert die Lektion „Methoden und der self-Parameter“?
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 Zig Academy-Lektion Code schreiben und ausführen?
Ja. Jede Zig Academy-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
- Structs und Felder deklarieren
- Methoden und der self-Parameter
- Standardwerte für Felder
- Anonyme Structs und Tupel