我正在尝试在派生类中添加共享成员并在基类中使用该值...
我有基地
class DBLayer
public shared function GetDetail(byval UIN as integer)
dim StrSql = string.format("select * from {0} where uin = {1}", tablename, uin)
....
end function
end class
我的派生类
class User inherits dblayer
public shared tabledname as string = "users"
end class
class item inherits dblayer
public shared tabledname as string = "item"
end class
class category inherits dblayer
public shared tabledname as string = "category"
end class
目前在我的基类中使用派生类的tablename
变量时出错。
答案 0 :(得分:0)
我要创建
Protected MustInherit Readonly Property TableName as string
在基类中。然后,每个子类都需要覆盖它并从属性中返回正确的名称。
因此,在其中一个子类中,它将是:
Protected OverRides Readonly Property TableName as string
Get
Return "users" ' or category or item etc
End Get
End Property
答案 1 :(得分:0)
Class User
Inherits DBLayer
Public Shared Shadows Function TableName() As String
Return "users"
End Function
Public Overrides Function GetTableName() As String
Return User.TableName
End Function
End Class
MustInherit Class DBLayer
MustOverride Function GetTableName() As String
Public Function GetDetail(ByVal UIN As Integer)
Return GetDetail(UIN, GetTableName)
End Function
Public Shared Function GetDetail(ByVal UIN As Integer, ByVal TableName As String)
Dim StrSql As String = String.Format("select * from {0} where uin = {1}", TableName, UIN)
Return StrSql
End Function
End Class
Class DBLayer
Private _tableName As String
Public Property TableName() As String
Get
Return _tableName
End Get
Set(ByVal value As String)
_tableName = value
End Set
End Property
Public Sub New(ByVal tableName As String)
_tableName = tableName
End Sub
Public Function GetDetail(ByVal UIN As Integer)
Return GetDetail(UIN, Me.TableName)
End Function
Public Shared Function GetDetail(ByVal UIN As Integer, ByVal TableName As String)
Dim StrSql As String = String.Format("select * from {0} where uin = {1}", TableName, UIN)
End Function
End Class