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

続きです。

前回は継承といいつつ何もも継承してませんでした。
Animalをinheritします。

    17  Public Class Dog : inherits Animal

これを追加して、以下のようになりました。

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

Assembly 'test, Version=0.0, Culture=neutral, PublicKeyToken=null' saved successfully to '/home/takk/vbtest/test.exe'.
Compilation successful
Compilation took 00:00:00.5088390
takk@deb9:~/vbtest$
takk@deb9:~/vbtest$ mono test.exe
秋田犬です
takk@deb9:~/vbtest$

次に、AnimalクラスのmynameをPublicに変更して、
Dogクラスではmynameを削除します。
Tellメソッドも削除します。

takk@deb9:~/vbtest$ cat -n test.vb
     1  Imports System.Console
     2
     3  Public Class Animal
     4          Public myname As String = "動物"
     5          Private msg As String = ""
     6          Sub New(myname As String)
     7                  Me.myname = myname
     8          End Sub
     9          Sub Tell()
    10                  WriteLine( myname & "です")
    11          End Sub
    12          Sub Bark()
    13                  WriteLine(msg)
    14          End Sub
    15  End Class
    16
    17  Public Class Dog : inherits Animal
    18          Private msg As String = ""
    19          Sub New(myname As String)
    20                  Me.myname = myname & "犬"
    21          End Sub
    22          Sub Bark()
    23                  WriteLine(msg)
    24          End Sub
    25  End Class

Dogクラスは、Animalクラスのメソッドが使えるので、Tellを実行すると、mynameにアクセスできます。

    27  Public Class TestClass
    28          <STAThread()> _
    29          Shared Sub Main()
    30                  dim akitaDog as new Dog("秋田")
    31                  akitaDog.Tell()
    32          End Sub
    33
    34  End Class

実行すると、

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

~省略~

takk@deb9:~/vbtest$ mono test.exe
秋田犬です
takk@deb9:~/vbtest$

同じ結果です。

Leave a Reply

Your email address will not be published. Required fields are marked *

CAPTCHA