Yöntemler ve self Parametresi
Davranışları yapılarınıza ekleyin.
Yöntemler ve self Parametresi, CoddyKit'te ücretsiz bir Zig Academy dersidir. Bu, 4 dersinin 2. dersidir. Aşağıdan dersin tamamını ücretsiz okuyabilir, sonra tarayıcıda yerleşik kod editörü ve 7/24 yapay zeka koçu ile uygulamalı olarak pratik yapabilirsin. Bu, Zig Academy öğrenme yolunun bir parçasıdır ve ilerlemeniz web ve CoddyKit uygulaması arasında senkronize olur. Zig Academy kursu toplamda 4 dersten oluşur.
Bu dersin bazı bölümleri henüz çevrilmemiş olup İngilizce olarak gösterilmektedir.
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. 🔧
Sıkça Sorulan Sorular
“Yöntemler ve self Parametresi” dersi ücretsiz mi?
Evet — “Yöntemler ve self Parametresi” dersin tüm metni burada web'de ücretsiz olarak okunabilir. Etkileşimli olarak pratik yapmak (yerleşik kod editörü ve 7/24 yapay zeka koçu) ve Zig Academy kursunun geri kalanını açmak için CoddyKit PRO'ya yükselt. Zig Academy kursu toplamda 4 dersten oluşur.
“Yöntemler ve self Parametresi” dersinde ne öğreneceğim?
Davranışları yapılarınıza ekleyin. Zig Academy ile uygulamalı kodu tarayıcıda doğrudan çalıştırarak pratik yaparsın ve 7/24 yapay zeka koçu dersi çalışırken sorularını yanıtlar.
Zig Academy öğrenmeye başlamak için deneyim gerekli mi?
Önceden deneyim gerekmez. CoddyKit'te Zig Academy, başlangıçtan ileri seviyeye kadar yapılandırıldığı için buradan başlayabilir veya başından başlayıp kendi hızında ilerleme yapabilirsin. Bu, 4 dersinin 2. dersidir.
“Yöntemler ve self Parametresi” dersi ne kadar sürer?
Çoğu CoddyKit dersi yaklaşık 5–10 dakika sürer. Her biri kısa ve etkileşimli olduğu için sabit ilerleme yaparsın ve web ile uygulama arasında tam olarak bıraktığın yerden devam edebilirsin.
Bu Zig Academy dersinde kod yazıp çalıştırabilir miyim?
Evet. Her Zig Academy dersi yerleşik bir kod editörü içerir, bu sayede tarayıcıda gerçek kod yazıp çalıştırabilir ve anlık yapay zeka geri bildirimi alırsın — yerel kurulum gerekli değildir.
Bu kursun tüm dersleri
- Yapıları ve Alanları Bildirme
- Yöntemler ve self Parametresi
- Varsayılan Alan Değerleri
- Adsız Yapılar ve Demetler