谁能发现这个 VB.Net 代码的问题吗?
我正在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
Inherits
放在新行或使用:
分隔类名和Inherits
语句:Put
Inherits
on new line or use:
to separate the class name andInherits
statement:您的 Inherits 关键字必须换行。 Microsoft 在其帮助和支持中记录了这一点。 http://support.microsoft.com/kb/307222
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
列表中的第一个错误只是最低行号的错误,它并不总是错误的实际原因。
在错误列表的更下方,您将在每个子类中看到(以及其他几个)另外三个错误,内容为
End ofstatementexpected.
。这是因为Class
和Inherits
是单独的语句,并且位于不同的行上:或者:
当您修复这些错误时,这些类实际上是从
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 beacuseClass
andInherits
are separate statements and go on separate lines:or:
When you fix these errors, the classes do actually inherit from
Car
, and your code works.