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

有人能发现这个VB.Net代码的问题吗?,vb.net,inheritance,factory-method,Vb.net,Inheritance,Factory Method,我正在用VB.Net编写一些代码,我希望能向同事们演示(更不用说让自己熟悉一点)各种设计模式——我对FactoryMethod模式有一个问题 这是我的密码: Namespace Patterns.Creational.FactoryMethod ''' <summary> ''' This is the Factory bit - the other classes are merely by way of an example... ''' </su

我正在用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
Namespace Patterns.Creational.FactoryMethod
''' 
''这是工厂位-其他类仅作为示例。。。
''' 
公营汽车制造厂
''' 
“CreateCar可以被声明为共享的(换句话说,一个类方法)——这并不重要。
''不要太担心CreateCar方法的内容-关键是它决定了哪种类型
应该创建car的“”,然后返回该car特定子类的新实例。
''' 
公共函数CreateCar()作为Car
将blnMondeoCondition设置为布尔值=False
Dim blnFocusCondition为布尔值=False
Dim BlnFistaCondition为布尔值=False
如果是blnMondeoCondition,那么
归还新蒙迪欧()
ElseIf Blnfocus条件
返回新焦点()
那么艾尔塞夫·布尔尼斯塔的情况呢
返回新的嘉年华()
其他的
抛出新的ApplicationException(“无法创建汽车…”)
如果结束
端函数
末级
公车
Public MustOverride ReadOnly Property Price()作为十进制
末级
公共级蒙迪欧继承汽车
Public ReadOnly将Property Price()替换为十进制
得到
返回17000
结束
端属性
末级
公共类焦点继承了汽车
Public ReadOnly将Property Price()替换为十进制
得到
返回14000
结束
端属性
末级
公共级嘉年华继承汽车
Public ReadOnly将Property Price()替换为十进制
得到
返回12000
结束
端属性
末级
结束命名空间
当我试图编译此文件时,CarFactory.CreateCar中出现错误(BC30311),告诉我无法将Fiesta、Mondeo和Focus转换为汽车。我不明白问题是什么——它们都是Car的子类

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

干杯


Martin.

继承
放在新行上,或使用
分隔类名和
继承
语句:

Public Class Mondeo
    Inherits Car
...


Public Class Focus
    Inherits Car
...


Public Class Fiesta
    Inherits Car
...

您的Inherits关键字必须位于新行。Microsoft在其帮助和支持中对此进行了记录

更改SavingsAccount类 定义如下,以便 SavingsAccount从帐户继承 (请注意,Inherits关键字必须 显示在新行上):


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

在错误列表的下一步,您将看到(在其他几个错误中)另外三个错误,分别在每个子类中显示
预期语句结束。
。这是因为
继承
是单独的语句,并且在单独的行上进行:

Public Class Mondeo
  Inherits Car
或:


当您修复这些错误时,这些类实际上是从
Car
继承的,并且您的代码可以正常工作。

谢谢您的回答-我知道这可能是我忽略的。干杯+1谢谢你。。。我对DotNet的大部分经验都是在C#中,所以我认为这很可能是语法问题。
Public Class Mondeo : Inherits Car