Dodawanie metod do struktury
Nadaj typowi określone zachowanie.
Dodawanie metod do struktury to bezpłatna lekcja Mojo Academy na CoddyKit. To lekcja 3 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 Mojo Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
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. 🧩
Często zadawane pytania
Czy lekcja „Dodawanie metod do struktury” jest bezpłatna?
Tak — pełny tekst „Dodawanie metod do struktury” 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 Mojo Academy, przejdź na CoddyKit PRO. Kurs Mojo Academy zawiera 4 lekcji w sumie.
Co nauczysz się w „Dodawanie metod do struktury”?
Nadaj typowi określone zachowanie. Ćwiczysz Mojo Academy 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ąć Mojo Academy?
Nie wymagamy żadnego doświadczenia. Mojo Academy 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 3 z 4.
Ile czasu zajmuje lekcja „Dodawanie metod do struktury”?
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 Mojo Academy?
Tak. Każda lekcja Mojo Academy 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
- Definiowanie struktury
- Pola i metoda __init__
- Dodawanie metod do struktury
- Struktury a klasy Pythona