Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion animal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,18 @@ def initialize(name)
def cry
"#{self.name}: #{voice}"
end
end
end

class Dog < Animal
def initialize(name)
super
@voice = "ワンワン"
end
end

class Cat < Animal
def initialize(name)
super
@voice = "ニャー"
end
end
39 changes: 37 additions & 2 deletions car.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ class Car
# 定数を追加
UP_SPEED = 10
DOWN_SPEED = 20
MAX_PASSENGERS = 4

@@count = 0
attr_accessor :number, :color
attr_accessor :number, :color, :passengers
# speedは外部から設定しないのでreaderで定義
# @speedが使えるようになる
attr_reader :speed
Expand All @@ -15,11 +16,19 @@ def initialize(number, color)
# @speedを初期化
@speed = 0
@@count += 1
# 運転手を追加
@passengers = 1
end

# 加速用のメソッド追加
def speed_up
@speed += UP_SPEED
if speed >= 50
puts "[ALERT]スピードが#{@speed}になりました。減速します。"
speed_down
elsif speed >= 30
puts "[ALERT]スピードが#{@speed}になりました。"
end
end

# 減速用のメソッド追加
Expand All @@ -32,7 +41,33 @@ def speed_down
end
end

# 乗車上限メソッドを追加
def get_on
if @passengers >= MAX_PASSENGERS
puts "乗車できません。この車の最大乗車人数は#{MAX_PASSENGERS}人です。"
else
@passengers += 1
puts "乗車しました"
end
end

def self.count
@@count # return @@countの略
end
end

def self.count_info
"Carクラスのクラス変数@@countは#{@@count}です。"
end


end

class TrackCar < Car
attr_reader :load_weight
def initialize(number, color, load_weight=500)
super number, color
@load_weight = load_weight
end

end