Einer struct Methoden hinzufügen
Geben Sie Ihrem Typ Verhalten.
Einer struct Methoden hinzufügen ist eine kostenlose Mojo Academy-Lektion auf CoddyKit. Dies ist Lektion 3 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 Mojo Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
Data Plus Behavior
So far your struct only holds data. A method is a function that lives inside the struct and gives your type behavior. 🚀
Methods Live Inside
You write a method by defining a function indented inside the struct, right alongside its fields and __init__.
struct Point:
var x: Int
var y: Int
fn show(self):
print(self.x, self.y)self Comes First
A method's first parameter is self, the instance it was called on. Through self the method reads and uses the struct's own fields.
fn show(self):
print(self.x)Calling a Method
You call a method with dot syntax on an instance. Mojo passes that instance in as self automatically for you.
var p = Point(3, 4)
p.show()Methods Can Return Values
Like any function, a method can return a result. Declare the return type after an arrow and hand back a value.
fn sum(self) -> Int:
return self.x + self.yMethods Take Arguments Too
After self, a method can accept more arguments, just like a normal function, to work with extra data the caller provides.
fn scaled(self, factor: Int) -> Int:
return self.x * factorRead-Only by Default
A plain self parameter is borrowed, so the method can read fields but not change them. This keeps your data safe.
Mutating with mut self
To let a method change the instance, mark the parameter mut self. Now assignments to self.x actually update the value.
fn move_right(mut self, step: Int):
self.x += stepMethods Build on Each Other
One method can call another through self, so you compose small behaviors into larger ones without repeating code.
fn report(self):
print(self.sum())Encapsulation in Action
Keeping fields and the methods that use them in one struct is encapsulation. The type owns both its data and how that data behaves.
A Struct That Does Work
Here a Point both stores coordinates and reports their sum. Your type is now active, not just a passive data container.
struct Point:
var x: Int
var y: Int
fn sum(self) -> Int:
return self.x + self.yQuick Check
Let us check what you know about methods.
Recap
You gave your struct methods that read and even mutate its fields. Next you will compare Mojo structs with Python classes. 🧩
Häufig gestellte Fragen
Ist die Lektion „Einer struct Methoden hinzufügen“ kostenlos?
Ja — der vollständige Text von „Einer struct Methoden hinzufügen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Mojo Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Mojo Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „Einer struct Methoden hinzufügen“?
Geben Sie Ihrem Typ Verhalten. Du übst Mojo 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 Mojo Academy zu starten?
Keine Vorkenntnisse erforderlich. Mojo 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 3 von 4.
Wie lange dauert die Lektion „Einer struct Methoden hinzufügen“?
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 Mojo Academy-Lektion Code schreiben und ausführen?
Ja. Jede Mojo 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
- Eine struct definieren
- Felder und die __init__-Methode
- Einer struct Methoden hinzufügen
- Structs und Python-Klassen im Vergleich