如何隐藏初始化的子类 [英] How Do I Hide Subclass From Being Initialised

查看:93
本文介绍了如何隐藏初始化的子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我目前正在创建一个新类,我正在努力隐藏子类并保护其不被直接初始化。



当我使用

Hi
I currently am creating a new class and i am struggling to get a subclass hidden and protected from being directly initialised.

When i use

Private TheClass as MyClass1



i也可以选择它的子类(clsClientInfo) - 我希望能够删除clsClientInfo并且只将它作为基类的一部分,可以任何人都帮助我,让我朝正确的方向发展。



i已经读取了继承,保护,mybase等的负载,但似乎无法理解这一点。




i can also select its subclass (clsClientInfo) - i want to be able to remove the clsClientInfo and only have it part of the base class, can anyone help me and poit me in the right direction please.

i have read loads on inherit, protect, mybase etc but cannot seem to get my head round this bit in particular.

Public Class MyClass1
    'Gain Properties from clsClientInfo
    Public CustomerInfo As clsClientInfo



    '######
    'SUB CLASS: clsClientInfo'
    '######

    Class clsClientInfo
        Private m_AC_ACCOUNT_NUMBER As Long
        Private m_AC_ACCOUNT_NAME As String
        Private m_AC_ACCOUNT_DOB As Date
        Private m_AC_ACCOUNT_EMAIL As Date
        Public Property AccountNum() As Long
            Get
                Return m_AC_ACCOUNT_NUMBER
            End Get
            Set(value As Long)
                m_AC_ACCOUNT_NUMBER = value
            End Set
        End Property
        Public Property FullName() As String
            Get
                Return m_AC_ACCOUNT_NAME
            End Get
            Set(value As String)
                m_AC_ACCOUNT_NAME = value
            End Set
        End Property
        Public Property DateOfBirth() As Date
            Get
                Return m_AC_ACCOUNT_DOB
            End Get
            Set(value As Date)
                m_AC_ACCOUNT_DOB = value
            End Set
        End Property
        Public Property EmailAddress() As Date
            Get
                Return m_AC_ACCOUNT_DOB
            End Get
            Set(value As Date)
                m_AC_ACCOUNT_DOB = value
            End Set
        End Property
    End Class
End Class





真的很感激它,



很多谢谢



Really Appreciate it,

many Thanks

推荐答案

任何类,而不是子类。这有两个方面。



首先,这个类可以是抽象的,无法通过定义实例化。



但是在某些情况下,您希望拥有该类的实例,但通过构造此类的实例,通过初始化变量或成员来保护类的用户免于直接实例化该类。这是通过创建没有可访问构造函数的类来完成的。怎么样?唯一棘手的事情是消除默认构造函数。为此,请定义不带参数的 private 构造函数。它不需要任何重要的主体/实现,它可以是空的。如果你这样做,那么这个类的任何初始化语句都会导致编译错误。



这些技术的目的是将这个类用作其他类的基类。可以实例化。在第二种技术中,这是允许以某种特殊方式(使用工厂方法)创建类实例的方法,而不是直接创建。此技术的变体将使用不同的访问修饰符,例如,允许仅为派生类创建实例,然后应使用 protected 访问修饰符;等等: http://msdn.microsoft.com/en-us/library/76453kax.aspx [ ^ ]。



-SA
Any class, not a "sub-class". There are two aspects of this.

First of all, this class can be abstract, which cannot be instantiated by definition.

But there are cases when you want to have instances of the class, but protect the user of the class from direct instantiation of the class through initialization of a variable or a member by constructing an instance of this class. This is done by creating a class without accessible constructors. How? The only tricky thing is to eliminate default constructor. For this purpose, define private constructor without parameters. It doesn't need any significant body/implementation, it can be empty. If you do so, any initialization statement of such class will cause compilation error.

The purpose of these techniques is to use this class as a base class for other classes which can be instantiated. In second technique, this is the way to allow creation of the instances of the class only in some special way (using factory methods), not directly. The variation of this technique would be using different access modifiers, for example, to allow creation of an instance only for derived class, then protected access modifier should be used; and so on: http://msdn.microsoft.com/en-us/library/76453kax.aspx[^].

—SA


这篇关于如何隐藏初始化的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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