diff --git a/animal.rb b/animal.rb index e179d06..01857a3 100644 --- a/animal.rb +++ b/animal.rb @@ -9,4 +9,18 @@ def initialize(name) def cry "#{self.name}: #{voice}" end -end \ No newline at end of file +end + + class Dog < Animal + def initialize(name) + super + @voice = "ワンワン" + end + end + + class Cat < Animal + def initialize(name) + super + @voice = "ニャー" + end + end \ No newline at end of file diff --git a/car.rb b/car.rb index 6ee3b7d..f37d7b3 100644 --- a/car.rb +++ b/car.rb @@ -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 @@ -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 # 減速用のメソッド追加 @@ -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 \ No newline at end of file + + 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 +