「Crystal」の版間の差分
削除された内容 追加された内容
→繰返し: Fix inner ln タグ: 2017年版ソースエディター |
→抽象クラス: Java/抽象クラスを、Crystalに移植しました。crystal tool format タグ: 2017年版ソースエディター |
||
401 行
[[JavaScript/クラス#包含と継承]]を、Rubyに移植した[[Ruby#包含と継承]]を、Crystalに移植しました。
;包含と継承の例:<syntaxhighlight lang=crystal line>
class Point
def initialize(@x = 0, @y = 0)
end def inspect(io)
io << "x:#{@x}, y:#{@y}"
end
def move(dx = 0, dy = 0)
@x, @y = @x + dx, @y + dy
415 ⟶ 420行目:
@location = Point.new(x, y)
end
def inspect(io)
@location.inspect(io)
end
def move(x, y)
@location.move(x, y)
422 ⟶ 431行目:
end
class Rectangle < Shape
def initialize(x = 0, y = 0, @width = 0, @height = 0)
super(x, y)
end
def inspect(io)
super(io)
433 ⟶ 443行目:
rct = Rectangle.new(12, 32, 100, 50)
p!
(END)</syntaxhighlight>
;実行結果:<syntaxhighlight lang=text>
rct # => x:12, y:32, width:100, height:50
446 ⟶ 456行目:
rct.move(11, 21) # => x:23, y:53, width:100, height:50
</syntaxhighlight>
;crystal tool hierarchy:<syntaxhighlight lang=console>
% crystal tool hierarchy inclusion-and-inheritance.cr -e Shape
- class Object (4 bytes)
|
+- class Reference (4 bytes)
|
+- class Shape (16 bytes)
. @location : Point (8 bytes)
|
+- class Rectangle (24 bytes)
@width : Int32 (4 bytes)
@height : Int32 (4 bytes)
</syntaxhighlight>
: crystal の tool hierarchy サブコマンドで、クラスの継承関係がわかります。
==== 抽象クラス ====
[[Java/抽象クラス]]を、Crystalに移植しました。
;抽象クラスの宣言:<syntaxhighlight lang=Java>
abstract class クラス名
#
end
</syntaxhighlight>
: このクラス名は、 .new でインスタンス化出来ません。
:: Error: can't instantiate abstract class クラス名
: となります。
:インスタンス化することは出来ませんが、抽象クラスを別のクラスが継承する事は出来ます。
:また、抽象クラスを <code>super()</code> を使うことでメソッドを呼び出せるので、抽象メソッドではないメソッド(具象メソッド)を持つことも、インスタンス変数も持つことも出来ます。
:'''抽象クラスの例'''では、Shapeのinitializeメソッドが抽象クラスの具象メソッドとなっています。
;抽象メソッドの宣言:<syntaxhighlight lang=Java>
abstract def メソッド名
</syntaxhighlight>
: 派生先のクラスで、「メソッド名」を定義(def)し忘れると
:: Error: abstract `def クラス名#メソッド名()` must be implemented by クラス名
: となります
;抽象クラスの例:<syntaxhighlight lang=crystal line>
abstract class Shape
def initialize(@x = 0.0, @y = 0.0)
end
abstract def to_s(io)
abstract def area
end
class Square < Shape
def initialize(x, y, @wh = 0.0)
super(x, y)
end
def to_s(io)
io << "Square(#{@x}, #{@y}, #{@wh})"
end
def area
@wh * @wh
end
end
abstract class Shape
def initialize(@x = 0.0, @y = 0.0)
end
abstract def to_s(io)
abstract def area
end
class Square < Shape
def initialize(x, y, @wh = 0.0)
super(x, y)
end
def to_s(io)
io << "Square(#{@x}, #{@y}, #{@wh})"
end
def area
@wh * @wh
end
end
class Recrangle < Shape
def initialize(x, y, @w = 0.0, @h = 0.0)
super(x, y)
end
def to_s(io)
io << "Rectanle(#{@x}, #{@y}, #{@w}, #{@h})"
end
def area
@w * @h
end
end
class Circle < Shape
def initialize(x, y, @r = 0.0)
super(x, y)
end
def to_s(io)
io << "Circle(#{@x}, #{@y}, #{@r})"
end
def area
3.1425926536 * @r * @r
end
end
shapes = [
Square.new(5.0, 10.0, 15.0),
Recrangle.new(13.0, 23.0, 20.0, 10.0),
Circle.new(3.0, 2.0, 20.0),
] of Shape
shapes.each do |shape|
puts("#{shape}: #{shape.area}")
end
</syntaxhighlight>
;実行結果:<syntaxhighlight lang=text>
Square(5.0, 10.0, 15.0): 225.0
Rectanle(13.0, 23.0, 20.0, 10.0): 200.0
Circle(3.0, 2.0, 20.0): 1257.03706144
</syntaxhighlight>
;crystal tool hierarchy:<syntaxhighlight lang=console>
% crystal tool hierarchy abstract.cr -e Shape
- class Object (4 bytes)
|
+- class Reference (4 bytes)
|
+- class Shape (24 bytes)
. @x : Float64 (8 bytes)
. @y : Float64 (8 bytes)
|
+- class Circle (32 bytes)
| @r : Float64 (8 bytes)
|
+- class Recrangle (40 bytes)
| @w : Float64 (8 bytes)
| @h : Float64 (8 bytes)
|
+- class Square (32 bytes)
@wh : Float64 (8 bytes)
</syntaxhighlight>
: crystal の tool hierarchy サブコマンドで、クラスの継承関係がわかります。
: 「包含と継承の例」と比べると、ShapeとRectangleが同じ階層にあることがわかると思います。
[TODO:virtual class::いい例がない]
== 制御構造 ==
|