-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance-ex1.rb
More file actions
72 lines (50 loc) · 959 Bytes
/
inheritance-ex1.rb
File metadata and controls
72 lines (50 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Vehicle
@@counters = 0
def initialize
@@counters += 1
end
def self.gas_miles(gas,miles)
puts "每公升的油可以行駛:#{miles.to_f/gas}"
end
private
def years_old
Time.now.year - self.year
end
end
module Capacity
def size(num)
num > 2000 ? puts "big" : puts "small"
end
end
class MyCar < Vehicle
attr_accessor :year, :color,:model
attr_reader :current_speed
@gas
@miles
@weight = 1300
def initialize(year,color,model)
@year=year
@color=color
@model=model
@current_speed=0
end
def speed_up(up_speed)
@current_speed+=up_speed
end
def brake(down_speed)
@current_speed-=down_speed
end
def shut_off
@current_speed=0
end
def spray_paint(car_color)
self.color = car_color
end
def to_s
puts "MyCar's color is #{color} , year is #{year} , model is #{model}"
end
end
class MyTruck < Vehicle
include Capacity
@weight = 5000
end