“覆盖"的方法子类中的共享成员 [英] Method to "override" shared Members in child classes

查看:17
本文介绍了“覆盖"的方法子类中的共享成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我正在尝试在 vb.net 中创建一种可用于创建/获取数据库条目的模型.

At the moment I'm trying to create a kind of model in vb.net which can be used to create/fetch database entrys.

我创建了一个带有共享函数的主类模型来获取数据集,例如模型.find().现在我想创建继承主模型类的类,例如一个单独的用户:UserModel.find() => "SELECT * FROM users".

I created a main class Model with a shared function to fetch the datasets, e.g. Model.find(). Now I'd like to create Classes which inherit the main Model-Class, e.g. a separate one for users: UserModel.find() => "SELECT * FROM users".

我现在需要的是找到一种方法来告诉类它应该使用哪个表.我想到了一个抽象字符串表",它是每个子模型"中的常量,但由于无法覆盖共享成员,如何实现?

What I need now is to find a way to tell the Class which table it should use. I thought about an abstract String "table" which is a constant in each "child-model", but how could this be implemented as it's not possible to override shared members?

提前致谢!

编辑:也许这会使我的意思更清楚一些:

Edit: Maybe this will make it a little clearer what I mean:

Public Class Model
Public Shared _controller As Controller

Public Shared table As String
Protected Shared tableFields As String()
Shared reader As Npgsql.NpgsqlDataReader

Public Shared Function find()
    Dim a As ArrayList = New ArrayList

    'Test if the tablefields are already known to the class, if not, get them

    If tableFields Is Nothing Then
        getTableFields()
    End If

    Dim query As String = "SELECT " + String.Join(", ", tableFields) + " FROM " + table
    reader = _controller.executeReader(query)
        While reader.Read
            o = New Model
            Dim v As New Hashtable
            For Each field In tableFields
                v(field) = reader(field)
            Next
            o.values = v
            a.Add(o)
        End While
        reader.Close()
        Return DirectCast(a.ToArray(GetType(Model)), Model())
End Function

Public values As Hashtable

Public Sub New()
End Sub

End Class

所以我想要一个共享方法来查找所有数据库条目并返回一个它自己类型的实例数组,例如模型().这就是为什么我想保持 find-method 共享而不是绑定到实例.

So I want a shared method which finds all database entries and gives back an array of instances of its own type, e.g. Model(). That's why I wanted to keep the find-method shared and not bound to an instance.

推荐答案

我认为你可以使用 Generics.这里我粘贴了一个例子

I think you could use Generics. Here I´ve pasted an example

您域中的所有类都可以继承自实体类

All the classes in your domain could inherit from Entity class

Public MustInherit Class Entity

    '...

End Class

您的模型类,使用您的方法 Find

Your Model class, with your method Find

Public Class Model

    Public Shared Sub Find(Of T As Entity)()

        ' You could know the name of T to find the table

        Dim tableName As String = GetType(T).Name

        '... 

    End Sub

End Class

域的一类,例如:用户类

Public Class User
    Inherits Entity

    ' ...

End Class

最后,如何实例化 Find 方法

Model.Find(Of User)()

'...

我不知道这是否是您的意思,您觉得这有帮助吗?

I dunno if this is what you mean, do you find this helpfull?

这篇关于“覆盖"的方法子类中的共享成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆