Skip to content

Instantly share code, notes, and snippets.

@darui00kara
Created May 17, 2015 08:37
Show Gist options
  • Save darui00kara/1942989acf27dd72c9ee to your computer and use it in GitHub Desktop.
Save darui00kara/1942989acf27dd72c9ee to your computer and use it in GitHub Desktop.
# encoding: utf-8
# File Name: main4.rb
# Create Day is 2015/05/17
# Last Update Day is 2015/05/17
# その他
class SuperOther
def initialize(private_text="private", protected_text="protected", public_text="public")
@private_text = private_text
@protected_text = protected_text
@public_text = public_text
end
# プライベートメソッド(定義クラス内のみ公開)
private
def private_method
puts "private_method"
end
# プロテクテッドメソッド(定義クラスとそのサブクラスに公開)
protected
def protected_method
puts "protected_method"
end
# パブリックメソッド(クラスの外へも公開)
public
def public_method
puts "public_method"
end
def print_text
puts "@private_text is " + @private_text
puts "@protected_text is " + @protected_text
puts "@public_text is " + @public_text
end
# アクセッサ
private
# クラス内のみ公開
attr_accessor :private_text
protected
# 定義クラスとサブクラスのみ公開
attr_accessor :protected_text
public
# クラスの外に公開
attr_accessor :public_text
end
class SubOther < SuperOther
def initialize()
super("hoge", "huge", "hogehoge")
end
# 子クラスでプロテクテッドのメソッドを呼び出す
def call_protected_method
self.protected_method
end
# アクセッサで定義されている変数に値を入れる
def set_blank_text
self.private_text = ""
self.protected_text = ""
self.public_text = ""
end
end
puts "main4"
puts
puts "SuperOther test"
super_other = SuperOther.new
# privateは呼び出せない
#super_other.private_method
# protectedは呼び出せない
#super_other.protected_method
# publicは呼び出せる
super_other.public_method
super_other.print_text
puts
puts "SubOhter test"
sub_other = SubOther.new
sub_other.public_method
sub_other.call_protected_method
puts
sub_other.print_text
sub_other.set_blank_text
sub_other.print_text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment