LoginSignup
0
0

More than 3 years have passed since last update.

オブジェクト、インスタンス、クラスについて

Posted at

オブジェクト

文字列、数値、変数、配列のこと
メソッドによって文字列、数値、変数、配列を操作することができる

オブジェクトは何らかのクラスに所属している
38Integerクラス
helloStringクラスに所属している

オブジェクトはクラスから作ることができる

インスタンス

クラスという設計図を元に作られたオブジェクト

オブジェクトがあるクラスに所属していることを強調したい時にインスタンスと言う

インスタンス ≒ オブジェクト

クラス

データと処理をまとめたもの

データ = インスタンス変数(クラス内で定義された変数)
処理 = インスタンスメソッド(クラスの中に書いたメソッドのこと)

インスタンス変数はクラスの中でしか使えない

class Student #クラス名
  def initialize(name)
    @name = name #インスタンス変数を定義
  end

  def avg(a,b) #インスタンスメソッド
    p @name,(a + b) / 2
  end
end

a = Student.new("sato") #オブジェクト化、オブジェクト生成、インスタンス化

a.avg(80,70) #メソッドの実行

オブジェクト生成
クラス名.new
→そのクラスのインスタンスメソッドが使えるようになる

0
0
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
0
0