代码之家  ›  专栏  ›  技术社区  ›  Martin Milan

有人能发现这个问题吗VB.Net版代码?

  •  4
  • Martin Milan  · 技术社区  · 14 年前

    我在写一些代码VB.Net版我希望向同事们展示(更不用说让自己熟悉一点)各种设计模式——我对FactoryMethod模式有一个问题。

    这是我的密码:

    Namespace Patterns.Creational.FactoryMethod
    
        ''' <summary>
        ''' This is the Factory bit - the other classes are merely by way of an example...
        ''' </summary>
        Public Class CarFactory
            ''' <summary>
            ''' CreateCar could have been declared as Shared (in other words,a Class method) - it doesn't really matter.
            ''' Don't worry too much about the contents of the CreateCar method - the point is that it decides which type
            ''' of car should be created, and then returns a new instance of that specific subclass of Car.
            ''' </summary>
            Public Function CreateCar() As Car
                Dim blnMondeoCondition As Boolean = False
                Dim blnFocusCondition As Boolean = False
                Dim blnFiestaCondition As Boolean = False
    
                If blnMondeoCondition Then
                    Return New Mondeo()
                ElseIf blnFocusCondition Then
                    Return New Focus()
                ElseIf blnFiestaCondition Then
                    Return New Fiesta()
                Else
                    Throw New ApplicationException("Unable to create a car...")
                End If
    
            End Function
        End Class
    
        Public MustInherit Class Car
            Public MustOverride ReadOnly Property Price() As Decimal
        End Class
    
        Public Class Mondeo Inherits Car
    
            Public ReadOnly Overrides Property Price() As Decimal
                Get
                    Return 17000
                End Get
            End Property
        End Class
    
        Public Class Focus Inherits Car
            Public ReadOnly Overrides Property Price() As Decimal
                Get
                    Return 14000
                End Get
            End Property
        End Class
    
        Public Class Fiesta Inherits Car
            Public ReadOnly Overrides Property Price() As Decimal
                Get
                    Return 12000
                End Get
            End Property
        End Class
    
    End Namespace
    

    毫无疑问,我忽略了一些简单的事情。有人能发现吗?

    干杯,

    马丁。

    3 回复  |  直到 14 年前
        1
  •  2
  •   William    14 年前

    您的Inherits关键字必须位于新行。微软在他们的帮助和支持中记录了这一点。 http://support.microsoft.com/kb/307222

    更改SavingsAccount类 定义如下,以便 SavingsAccount从帐户继承 (请注意,Inherits关键字必须

        2
  •  4
  •   Darin Dimitrov    14 年前

    Inherits 在新的线路上或使用 : 将类名和 继承 声明:

    Public Class Mondeo
        Inherits Car
    ...
    
    
    Public Class Focus
        Inherits Car
    ...
    
    
    Public Class Fiesta
        Inherits Car
    ...
    
        3
  •  1
  •   Guffa    14 年前

    列表中的第一个错误只是最低行号处的错误,它并不总是错误的实际原因。

    End of statement expected. 在每个子类中。这是因为 Class Inherits 是单独的语句,并在单独的行上:

    Public Class Mondeo
      Inherits Car
    

    Public Class Mondeo : Inherits Car
    

    当您修复这些错误时,类实际上是从 Car ,代码正常工作。