gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 00:33:48 24 4
gpt4 key购买 nike

我正在 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 的子类。

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

干杯,

马丁。

最佳答案

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

Public Class Mondeo
Inherits Car
...


Public Class Focus
Inherits Car
...


Public Class Fiesta
Inherits Car
...

关于vb.net - 谁能发现这个 VB.Net 代码的问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3694397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com