Linux(mono)でVB、継承(その7)

前回ただの犬になってしまった理由を突き止めるため、
Console.WriteLineをNewの入り口と出口にいれて確認します。

    14  Public Class Dog : inherits Animal
    15          Public myname As String = "犬"
    16          Public msg As String = "ワンワン"
    17          Sub New()
    18                  System.Console.WriteLine("Dog New")
    19                  MyBase.myname = Me.myname
    20                  MyBase.msg= Me.msg
    21                  System.Console.WriteLine("Dog New End")
    22          End Sub
    23  End Class
    24
    25  Public Class Doberman : inherits Dog
    26          Public myname As String = "Doberman"
    27          Public msg As String = "Bowwow"
    28          Sub New()
    29                  System.Console.WriteLine("Doberman New")
    30                  MyBase.myname = Me.myname
    31                  MyBase.msg= Me.msg
    32                  System.Console.WriteLine("Doberman New End")
    33          End Sub
    34  End Class
    35
    36  Public Class TestClass
    37          <STAThread()> _
    38          Shared Sub Main()
    39                  dim doberman as new Doberman()
    40                  doberman.Tell()
    41                  doberman.Bark()
    42          End Sub
    43
    44  End Class

DogとDobermanのNewはどちらか早く実行されるのでしょうか。

takk@deb9:~/vbtest$ vbnc test.vb

~省略~

takk@deb9:~/vbtest$ mono test.exe
Dog New
Dog New End
Doberman New
Doberman New End
犬です
ワンワン
takk@deb9:~/vbtest$

DogのNewの実行が終わってから、DobermanのNewが実行されています。

ん~。先に親のNewが実行されるなら、Newの中で変数の書き換えをしていては、ずっとドーベルマンになってくれません。Newの引数で渡してみてはどうでしょうか。

takk@deb9:~/vbtest$ cat -n test.vb
     1  Imports System.Console
     2
     3  Public Class Animal
     4          Public myname As String = "動物"
     5          Public msg As String = ""
     6          Sub Tell()
     7                  WriteLine( myname & "です")
     8          End Sub
     9          Sub Bark()
    10                  WriteLine(msg)
    11          End Sub
    12  End Class
    13
    14  Public Class Dog : inherits Animal
    15          Public myname As String = "犬"
    16          Public msg As String = "ワンワン"
    17          Sub New(myname as String, msg as String)
    18                  MyBase.myname = myname
    19                  MyBase.msg= msg
    20          End Sub
    21  End Class
    22
    23  Public Class Doberman : inherits Dog
    24          Public myname As String = "Doberman"
    25          Public msg As String = "Bowwow"
    26          Sub New()
    27                  MyBase.New(Me.myname , Me.msg)
    28          End Sub
    29  End Class
    30
    31  Public Class TestClass
    32          <STAThread()> _
    33          Public Sub Main()
    34                  dim doberman as new Doberman()
    35                  doberman.Tell()
    36                  doberman.Bark()
    37          End Sub
    38
    39  End Class
takk@deb9:~/vbtest$

ビルド。

takk@deb9:~/vbtest$ vbnc test.vb
Visual Basic.Net Compiler version 0.0.0.5943 (Mono 4.0.1 - tarball)
Copyright (C) 2004-2010 Rolf Bjarne Kvinge. All rights reserved.

vbnc : error VBNC30737: No accessible 'Main' method with an appropriate signature was found in 'test'.
There were 1 errors and 0 warnings.
Compilation took 00:00:00.5038970
takk@deb9:~/vbtest$

なんと、エラーになりました。

オブジェクト指向、理解がまだまだ足りません。

コメント

タイトルとURLをコピーしました