1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

classについてのメモ(Ruby)

1
Posted at
obj_test.rb
# -*- coding: utf-8 -*-

# クラス名は大文字で始める
class Fighter
    
    #class内の関数をメソッドと呼ぶ
    #initでclassの初期化(使用する変数の設定?)
    def initialize(attack_point)
        #インスタンス変数
        @attack_point = attack_point
    end

    def attack
        puts "プレイヤーの攻撃!" + @attack_point.to_s + "ダメージを与えた!"
    end

end

# インスタンスを作成
player = Fighter.new(50)
player.attack


# Fighterクラスを継承
class Berserker < Fighter

    def attack
      puts "プレイヤーの攻撃! 互いに" + (@attack_point * 2).to_s + "ダメージを受けた!"
    end

end

player = Berserker.new(50)
player.attack

結果

プレイヤーの攻撃!50ダメージを与えた!
プレイヤーの攻撃! 互いに100ダメージを受けた!

  
参考図書
アジャイル時代のオブジェクト脳のつくり方 Rubyで学ぶ究極の基礎講座

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?