VBで継承を覚えてます。
AnimalでもっているメソッドはDogから完全に削除します。
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 Sub New(myname As String) 19 Me.myname = myname & "犬" 20 End Sub 21 End Class 22 23 Public Class TestClass 24 <STAThread()> _ 25 Shared Sub Main() 26 dim akitaDog as new Dog("秋田") 27 akitaDog.Tell() 28 End Sub 29 30 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.5196550 takk@deb9:~/vbtest$ mono test.exe 秋田犬です takk@deb9:~/vbtest$
大丈夫のようです。
吠えさえてみます。
23 Public Class TestClass 24 <STAThread()> _ 25 Shared Sub Main() 26 dim akitaDog as new Dog("秋田") 27 akitaDog.Tell() 28 akitaDog.Bark() 29 End Sub 30 31 End Class takk@deb9:~/vbtest$
実行。
takk@deb9:~/vbtest$ vbnc test.vb ~省略~ takk@deb9:~/vbtest$ mono test.exe 秋田犬です takk@deb9:~/vbtest$
あれ、吠えません。
これでどうでしょうか。変更箇所は、5行目と18行目です。
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 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 Public msg As String = "ワンワン" 19 Sub New(myname As String) 20 Me.myname = myname & "犬" 21 End Sub 22 End Class takk@deb9:~/vbtest$
実行。
takk@deb9:~/vbtest$ vbnc test.vb ~省略~ takk@deb9:~/vbtest$ mono test.exe 秋田犬です takk@deb9:~/vbtest$
吠えませんねえ。
コメント