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

发布于 2024-09-18 09:35:24 字数 2204 浏览 2 评论 0原文

我正在 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

当我尝试编译此代码时,我在 CarFactory.CreateCar 中收到错误 (BC30311),告诉我它无法将嘉年华、蒙迪欧和福克斯转换为汽车。我不明白问题是什么 - 它们都是 Car 的子类。

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

干杯,

马丁。

I'm writing some code in VB.Net which I hope demonstrate to colleagues (not to say familiarise myself a little more) with various design patterns - and I'm having an issue with the FactoryMethod Pattern.

Here's my code:

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

When I try to compile this, I am getting errors (BC30311) in the CarFactory.CreateCar telling me that it can't convert Fiesta, Mondeo, and Focus into Car. I don't see what the issue is - they're all subclasses of Car.

Doubtless I'm overlooking something simple. Can anyone spot it?

Cheers,

Martin.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

忱杏 2024-09-25 09:35:24

Inherits 放在新行或使用 : 分隔类名和 Inherits 语句:

Public Class Mondeo
    Inherits Car
...


Public Class Focus
    Inherits Car
...


Public Class Fiesta
    Inherits Car
...

Put Inherits on new line or use : to separate the class name and Inherits statement:

Public Class Mondeo
    Inherits Car
...


Public Class Focus
    Inherits Car
...


Public Class Fiesta
    Inherits Car
...
呢古 2024-09-25 09:35:24

您的 Inherits 关键字必须换行。 Microsoft 在其帮助和支持中记录了这一点。 http://support.microsoft.com/kb/307222

更改 SavingsAccount 类
定义如下,使得
SavingsAccount 继承自 Account
(请注意,Inherits 关键字必须
出现在新行上):

Your Inherits keyword must be on a new line. This is documented by Microsoft on their help and Support. http://support.microsoft.com/kb/307222

Change the SavingsAccount class
definition as follows, so that
SavingsAccount inherits from Account
(note that the Inherits keyword must
appear on a new line):

沫离伤花 2024-09-25 09:35:24

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

在错误列表的更下方,您将在每个子类中看到(以及其他几个)另外三个错误,内容为 End ofstatementexpected.。这是因为 ClassInherits 是单独的语句,并且位于不同的行上:

Public Class Mondeo
  Inherits Car

或者:

Public Class Mondeo : Inherits Car

当您修复这些错误时,这些类实际上是从 Car 继承的>,你的代码就可以工作了。

The first error in the list is just the one at the lowest line number, it's not always the actual cause of the error.

Further down in the list of errors you will see (among several others) three more errors saying End of statement expected. at each of the subclasses. This is beacuse Class and Inherits are separate statements and go on separate lines:

Public Class Mondeo
  Inherits Car

or:

Public Class Mondeo : Inherits Car

When you fix these errors, the classes do actually inherit from Car, and your code works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文