【Ruby】クラスとインスタンスについて(メモ)
クラスとインスタンス
クラス・・・あるものを作る設計図のようなもの
インスタンス・・・クラスから生成されるもの(オブジェクト)
(具体例)
①クラス⇨車
インスタンス⇨ハンドル、ブレーキ、タイヤ、ライトなど
(またはスポーツカー、救急車、トラックなど)
②クラス⇨家
インスタンス⇨玄関、キッチン、洗面所、階段など
インスタンス作成
①Carクラスを作成
②initializeメソッドと種類を表示するtypeメソッドをクラス内に作成
③インスタンス(sports_car、ambulance、track)作成
④インスタンスに情報(メソッド)を与える。
以下、作成例
class Car
def initialize(name)
@name = name
end
def type
puts "私は#{@name}が好きです"
end
end
sports_car = Car.new("スポーツカー")
ambulance = Car.new("救急車")
track = Car.new("トラック")
sports_car.type
ambulance.type
track.type
#出力↓
#私はスポーツカーが好きです
#私は救急車が好きです
#私はトラックが好きです
以上
Author And Source
この問題について(【Ruby】クラスとインスタンスについて(メモ)), 我々は、より多くの情報をここで見つけました https://qiita.com/tech_kento/items/64e59020c24b408b2808著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .