「Crystal」の版間の差分
削除された内容 追加された内容
→包含と継承: JavaScript/クラス#包含と継承を、Rubyに移植したRuby#包含と継承を、Crystalに移植しました。 タグ: 2017年版ソースエディター |
→包含と継承: 「Crystalらしいコード」に変更 タグ: 2017年版ソースエディター |
||
403 行
;包含と継承:<syntaxhighlight lang=crystal line>
class Point
def initialize(@x = 0, @y = 0)
p("Point::
end
def move(dx = 0, dy = 0)
@x, @y
p("Point::move")
self
419 ⟶ 416行目:
def initialize(x = 0, y = 0)
@location = Point.new(x, y)
p("Shape::
end
def move(x, y)
429 ⟶ 426行目:
class Rectangle < Shape
def initialize(x = 0, y = 0, @width = 0, @height = 0)
super(x, y)
▲ p("Rectangle::constructor")
end
end
439 ⟶ 434行目:
p("Create a Rectangle!")
rct = Rectangle.new(12, 32, 100, 50)
p
p
p! rct.is_a?( p
p("rct.is_a?( Point ) => ", rct.is_a?(Point))▼
rct.move(11, 21)
p
rct2 = Rectangle.new(1, 2, 10, 150)
p
</syntaxhighlight>
;実行結果:<syntaxhighlight lang=text>
"Create a Rectangle!"
"Point::
"Shape::
"Rectangle::
rct # => #<Rectangle:
▲#<Rectangle:0x80096de40 @location=#<Point:0x80096ee60 @x=12, @y=32>, @width=100, @height=50>
▲"rct.is_a?( Shape ) => "
"Point::move"
"Shape::move"
rct # => #<Rectangle:
▲#<Rectangle:0x80096de40 @location=#<Point:0x80096ee60 @x=23, @y=53>, @width=100, @height=50>
▲"Point::constructor"
▲"Shape::constructor"
rct # => #<Rectangle:
▲"Rectangle::constructor"
rct2 # => #<Rectangle:
▲#<Rectangle:0x80096de40 @location=#<Point:0x80096ee60 @x=23, @y=53>, @width=100, @height=50>
▲#<Rectangle:0x80096de20 @location=#<Point:0x80096ee50 @x=1, @y=2>, @width=10, @height=150>
</syntaxhighlight>
|