0Pricing
Mojo Academy · レッスン

Structへのメソッド追加

型に振る舞いを持たせます

「Structへのメソッド追加」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.y

Methods 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 * factor

Read-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 += step

Methods 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.y

Quick 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. 🧩

よくある質問

「Structへのメソッド追加」レッスンは無料ですか?

はい。「Structへのメソッド追加」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「Structへのメソッド追加」で何を学びますか?

型に振る舞いを持たせます ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「Structへのメソッド追加」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. structの定義
  2. フィールドと__init__メソッド
  3. Structへのメソッド追加
  4. StructとPythonクラスの違い
← Mojo Academyに戻る