【案内】小説『エクストリームセンス』について

 小説『エクストリームセンス』は、本ブログを含めていくつか掲載していますが、PC、スマフォ、携帯のいずれでも読みやすいのは、「小説家になろう」サイトだと思います。縦書きのPDFをダウンロードすることもできます。

 小説『エクストリームセンス』のURLは、 http://ncode.syosetu.com/n7174bj/

2011年9月24日土曜日

訂正記事:オブジェクト指向の基本的効用

昨日アップした「オブジェクト指向の基本的効用」の中で誤りを見つけましたので訂正します。

同記事の中で、

「下記コードのように、構造体では実現できなかった振る舞いをクラスは実現してくれます。」

と記述していますが、赤字部分が間違いでした。
下記コードのように、VB.NETの構造体は振る舞いも実装できます。
詳しくはMSDNのStructure ステートメントに関する説明を参照してください。


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
 'VB.NET構造体へのプロパティ、プロシージャの実装例
    Private Structure PersonStructure2
‌ 
        Friend ID As Integer
        Friend Name As String
        Friend Birthday As Date
‌ 
        Friend ReadOnly Property Age() As Integer
            Get
                Dim _year As Integer =
                CType(Today.ToString("yyyy"), Integer)
                Dim _MMdd As Integer =
                    CType(Today.ToString("MMdd"), Integer)
                Dim _birthYear As Integer =
                    CType(Birthday.ToString("yyyy"), Integer)
                Dim _birthMMdd As Integer =
                    CType(Birthday.ToString("MMdd"), Integer)
                Dim _age As Integer = _year - _birthYear
                If _MMdd < _birthMMdd Then
                    _age = _age - 1
                End If
                Return _age
            End Get
        End Property
‌ 
        Friend Sub Initialize()
            ID = 0
            Name = ""
            Birthday = Nothing
        End Sub
‌ 
    End Structure
‌ 
    'PersonStructure2の使用
    Public Sub Test()
‌ 
        Dim foo As PersonStructure2
        With foo
            .ID = 1
            .Name = "Satohru"
            .Birthday = #5/25/1967#
        End With
        MessageBox.Show(foo.Age)
‌ 
        foo.Initialize()
        MessageBox.Show(foo.Name)
‌ 
    End Sub

0 件のコメント:

コメントを投稿