子类化 [英] subclassing

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

问题描述

您好。


我正在试图弄清楚如何创建具有特定属性的子类

到子类,到目前为止它还没有'''好的。


现在我有一个带有表示类型的枚举的类。该类具有特定于所有类型的所有属性

,但我想要的是

将这些属性移动到特定于每种类型的子类。


我的代码如下:


朋友类ValidateValue

私有_textMaxLen作为整数

私有_textMinLen作为整数

私有_validateType作为validateTypes


Friend Sub New(ByVal验证为字符串)

Dim compoundType As String

Dim remainder As String

Dim openParenPos As Integer

Dim commaPos As Integer


openParenPos = InStr(验证,"(")

如果openParenPos 0那么

compoundType = Mid(验证,1,openParenPos - 1)

余数= Mid (验证,openParenPos + 1)

余数=中(余数,1,Len(余数) - 1)''剥离结束paren

选择案例compoundType

Case" text"," textnum"

如果compoundType =" text"然后

_validateType = validateTypes.text

否则

_validateType = validateTypes.textNum

结束如果

commaPos = InStr(余数,&,,)

如果commaPos = 0那么

_textMaxLen = CInt(余数)

其他

_textMaxLen = CInt(中(余数,1,逗号 - 1))

_textMinLen = CInt(中(余数,逗号+ 1))

结束如果


但由于maxLen和minLen属性特定于两个文本

类型,我想要做的是声明一个子类它继承自

现有类,并包含和设置给定compoundType和余数的validateType,textMaxLen和

textMinLen属性。


我遇到的问题是子类的构造函数。在子类中,

我想传递复合类型和余数,并且代码如下:

''朋友类ValidateText

''继承ValidateValue


''Private _textMaxLen As Integer

''Private _textMinLen As Integer

''Private _validateType as validateTypes


''Friend Sub New(ByVal compoundType As String,ByVal remaining As String)

''Dim commaPos As Integer

''如果compoundType =" text"然后

''_ validateType = validateTypes.text

''否则

''_ validateType = validateTypes.textNum

''结束如果

''commaPos = InStr(余数,,)

''如果commaPos = 0那么

' '_textMaxLen = CInt(余数)

''否则

''_ textMaxLen = CInt(中(余数,1,逗号 - 1))

''_ textMinLen = CInt(中(余数,逗号+ 1))

''结束如果

''结束子


但是这不起作用,因为基类没有构造函数

没有参数。我想在基类中使用一个参数作为键来确定要创建的子类,然后创建一个子类。


我''在基类中思考我需要一个变量,如:

Private _child as ValidateValue


在基类的构造函数中,代码如下:

选择案例compoundType

案例" text"," textnum"

_child = new ValidateText(compoundType,remaining)


但我不能让子类工作。


我应该做些什么不同的工作?


感谢您的帮助,


-Beth

Hello.

I''m trying to figure out how to create subclasses with properties specific
to the subclass and so far it isn''t going well.

Right now I have a class with an enum representing the type. The class has
all the properties specific to all the types, but what I want instead is to
move those properties to subclasses specific to each type.

I have code like this:

Friend Class ValidateValue
Private _textMaxLen As Integer
Private _textMinLen As Integer
Private _validateType As validateTypes

Friend Sub New(ByVal validate As String)
Dim compoundType As String
Dim remainder As String
Dim openParenPos As Integer
Dim commaPos As Integer

openParenPos = InStr(validate, "(")
If openParenPos 0 Then
compoundType = Mid(validate, 1, openParenPos - 1)
remainder = Mid(validate, openParenPos + 1)
remainder = Mid(remainder, 1, Len(remainder) - 1) '' strip closing paren
Select Case compoundType
Case "text", "textnum"
If compoundType = "text" Then
_validateType = validateTypes.text
Else
_validateType = validateTypes.textNum
End If
commaPos = InStr(remainder, ",")
If commaPos = 0 Then
_textMaxLen = CInt(remainder)
Else
_textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
_textMinLen = CInt(Mid(remainder, commaPos + 1))
End If

But since the maxLen and minLen properties are specific to the two text
types, what I want to do is declare a subclass which inherits from the
existing class and contains and sets the validateType, textMaxLen, and
textMinLen properties given the compoundType and remainder.

The problem I''m having is with the subclass''s constructor. In the subclass,
I want to pass the compoundType and remainder, and have code like:
''Friend Class ValidateText
'' Inherits ValidateValue

'' Private _textMaxLen As Integer
'' Private _textMinLen As Integer
'' Private _validateType As validateTypes

'' Friend Sub New(ByVal compoundType As String, ByVal remainder As String)
'' Dim commaPos As Integer
'' If compoundType = "text" Then
'' _validateType = validateTypes.text
'' Else
'' _validateType = validateTypes.textNum
'' End If
'' commaPos = InStr(remainder, ",")
'' If commaPos = 0 Then
'' _textMaxLen = CInt(remainder)
'' Else
'' _textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
'' _textMinLen = CInt(Mid(remainder, commaPos + 1))
'' End If
'' End Sub

But that doesn''t work because the base class doesn''t have a constructor
without parameters. I want to use a parameter into a base class as a key to
determine the subclass to create and then create one of my subclasses.

I''m thinking in the base class I need a variable like:
Private _child as ValidateValue

And in the base class''s constructor, have code like:
Select Case compoundType
Case "text", "textnum"
_child = new ValidateText(compoundType, remainder)

But I can''t get the subclass to work.

What should I be doing differently?

Thanks for any help,

-Beth

推荐答案

嗯,你有没有理由'

ValidateValue中是否需要空构造函数?你不一定要打电话。在那之后,似乎

就像你可能意味着_textMaxLen,_textMinLen和_validateType

受到保护,而非私人。


"贝丝" < Be ** @ discuss.microsoft.com写信息

news:05 ************************** ******** @ microsof t.com ...
Well, is there a reason you don''t want an empty constructor in
ValidateValue? You don''t necessarily have to call it. After that, it seems
like you probably meant to have _textMaxLen, _textMinLen and _validateType
be protected, not private.

"Beth" <Be**@discussions.microsoft.comwrote in message
news:05**********************************@microsof t.com...

你好。


我是试图找出如何创建具有特定属性

的子类到子类,到目前为止它并不顺利。


现在我有一个带有表示类型的枚举的类。班级

有所有类型的所有属性

,但我想要的是



将这些属性移动到特定于每种类型的子类。


我的代码如下:


朋友类ValidateValue

Private _textMaxLen As Integer

Private _textMinLen As Integer

Private _validateType as validateTypes


Friend Sub New(ByVal validate As String )

Dim compoundType As String

Dim remainder As String

Dim openParenPos As Integer

Dim commaPos As Integer


openParenPos = InStr(验证,"(")

如果openParenPos 0那么

compoundType = Mid(验证,1 ,openParenPos - 1)

余数=中(验证,openParenPos + 1)

余数=中(余数,1,Len(余数) - 1)''剥离结束paren

Select Case compoundType

Case" text"," textn嗯

如果compoundType =" text"然后

_validateType = validateTypes.text

否则

_validateType = validateTypes.textNum

结束如果

commaPos = InStr(余数,&,,)

如果commaPos = 0那么

_textMaxLen = CInt(余数)

其他

_textMaxLen = CInt(中(余数,1,逗号 - 1))

_textMinLen = CInt(中(余数,逗号+ 1))

结束如果


但由于maxLen和minLen属性特定于两个文本

类型,我想要做的是声明一个子类它继承自

现有类,并包含和设置给定compoundType和余数的validateType,textMaxLen和

textMinLen属性。


我遇到的问题是子类的构造函数。在

子类中,

我想传递compoundType和余数,并且代码如下:

''Friend Class ValidateText
''继承ValidateValue


''私有_textMaxLen为整数

''私有_textMinLen为整数

' 'private _validateType as validateTypes


''Friend Sub New(ByVal compoundType As String,ByVal remaining As String)

''Dim commaPos As Integer

''如果compoundType =" text"然后

''_ validateType = validateTypes.text

''否则

''_ validateType = validateTypes.textNum

''结束如果

''commaPos = InStr(余数,,)

''如果commaPos = 0那么

' '_textMaxLen = CInt(余数)

''否则

''_ textMaxLen = CInt(中(余数,1,逗号 - 1))

''_ textMinLen = CInt(中(余数,逗号+ 1))

''结束如果

''结束子


但是这不起作用,因为基类没有构造函数

没有参数。我想在一个基类中使用一个参数作为键



确定要创建的子类然后创建我的子类之一。

我在基类中思考我需要一个变量,如:

Private _child as ValidateValue


在基类中的构造函数,代码如下:

Select Case compoundType

Case" text"," textnum"

_child = new ValidateText(复合类型,余数)


但是我不能让子类工作。


我应该采用不同的方式做什么?


感谢您的帮助,


-Beth
Hello.

I''m trying to figure out how to create subclasses with properties specific
to the subclass and so far it isn''t going well.

Right now I have a class with an enum representing the type. The class
has
all the properties specific to all the types, but what I want instead is
to
move those properties to subclasses specific to each type.

I have code like this:

Friend Class ValidateValue
Private _textMaxLen As Integer
Private _textMinLen As Integer
Private _validateType As validateTypes

Friend Sub New(ByVal validate As String)
Dim compoundType As String
Dim remainder As String
Dim openParenPos As Integer
Dim commaPos As Integer

openParenPos = InStr(validate, "(")
If openParenPos 0 Then
compoundType = Mid(validate, 1, openParenPos - 1)
remainder = Mid(validate, openParenPos + 1)
remainder = Mid(remainder, 1, Len(remainder) - 1) '' strip closing paren
Select Case compoundType
Case "text", "textnum"
If compoundType = "text" Then
_validateType = validateTypes.text
Else
_validateType = validateTypes.textNum
End If
commaPos = InStr(remainder, ",")
If commaPos = 0 Then
_textMaxLen = CInt(remainder)
Else
_textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
_textMinLen = CInt(Mid(remainder, commaPos + 1))
End If

But since the maxLen and minLen properties are specific to the two text
types, what I want to do is declare a subclass which inherits from the
existing class and contains and sets the validateType, textMaxLen, and
textMinLen properties given the compoundType and remainder.

The problem I''m having is with the subclass''s constructor. In the
subclass,
I want to pass the compoundType and remainder, and have code like:
''Friend Class ValidateText
'' Inherits ValidateValue

'' Private _textMaxLen As Integer
'' Private _textMinLen As Integer
'' Private _validateType As validateTypes

'' Friend Sub New(ByVal compoundType As String, ByVal remainder As String)
'' Dim commaPos As Integer
'' If compoundType = "text" Then
'' _validateType = validateTypes.text
'' Else
'' _validateType = validateTypes.textNum
'' End If
'' commaPos = InStr(remainder, ",")
'' If commaPos = 0 Then
'' _textMaxLen = CInt(remainder)
'' Else
'' _textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
'' _textMinLen = CInt(Mid(remainder, commaPos + 1))
'' End If
'' End Sub

But that doesn''t work because the base class doesn''t have a constructor
without parameters. I want to use a parameter into a base class as a key
to
determine the subclass to create and then create one of my subclasses.

I''m thinking in the base class I need a variable like:
Private _child as ValidateValue

And in the base class''s constructor, have code like:
Select Case compoundType
Case "text", "textnum"
_child = new ValidateText(compoundType, remainder)

But I can''t get the subclass to work.

What should I be doing differently?

Thanks for any help,

-Beth


你好,贝丝,
您可以调用基类的构造函数(带有

validate参数的参数)作为继承类的第一个可执行行

构造函数。


另外,正如Mike所说,你可能想要提及d变量是

受保护(非私有)然后从

继承类中删除相应的声明。


干杯,

兰迪

Hello, Beth,

You can call your base class''s constructor (with an argument for the
validate parameter) as the first executable line of the inherited classes
constructor.

Also, as Mike says, you probably want the mentioned variables to be
Protected (not private) and then remove the corresponding declarations from
the inherited class.

Cheers,
Randy


ftMike。

感谢您的回复。


我在两个类中都重命名了构造函数,但是它没有像我预期的那样工作。


现在我有了:

朋友类ValidateValue

私有_child为ValidateValue

....

朋友子初始化(ByVal验证作为String)

Dim compoundType As String

Dim remainder As String

Dim openParenPos As Integer

Dim commaPos As整数


openParenPos = InStr(验证,"(")

如果openParenPos 0则

compoundType = Mid(验证,1,openParenPos - 1)

余数=中(验证,openParenPos + 1)

余数=中(余数,1,Len(余数) - 1)''小号行程结束paren

选择案例compoundType

案例text,textnum

_child =新的ValidateValueText

_child.inittext(compoundType,remainder)''这个不编译

....

朋友类ValidateValueText

继承ValidateValue


私有_textMaxLen为整数

私有_textMinLen为整数

私有_validateType为validateTypes


Friend Sub initText(ByVal compoundType As String,ByVal remaining As String)

....


但ValidateValue中的_child对象就像一个

intellisense中的ValidateValue对象(声明它的方式),而不是ValidateValueText对象(

实例化它的方式。)


所以我有一个'是'a''的关系,就像''狗是哺乳动物','我正在尝试

来弄清楚如何将一个哺乳动物ID传递给哺乳动物课,如果是' 's
决定成为一只狗,然后创建一个Dog对象的实例,使得调用者可以使用
,并将mammalType属性设置为''dog'' 。


我所拥有的大致相当于:

朋友级哺乳动物

私人_儿童作为哺乳动物

....

朋友子初始化(ByVal mammalID as String)

Dim compoundType As String

Dim remainder As String

Dim openParenPos As Integer

Dim commaPos As Integer


openParenPos = InStr(mammalID,"(")

如果openParenPos 0那么

compoundType = Mid(mammalID,1,openParenPos - 1)

余数=中(哺乳动物ID,openParenPos + 1)

余数=中(余数,1,Len(余数) - 1)''剥离结束paren

选择案例compoundType

案例dog," wolf"

_child = New MammalDog

_child.initDog(compoundType,remainder)

....

朋友级MammalDog

继承哺乳动物


私有_isWild为布尔

私有_mammalType作为哺乳动物类型


朋友子initDog(ByVal复合类型为字符串,ByVal余数为字符串)

---

_child.initDog(compoundType,remainder)不会编译,因为它没有看到
initDog作为_child对象的方法。它可以看到哺乳动物的所有属性和

方法。


我想避免的是有类似的东西:

朋友级哺乳动物

私人_childDog作为MammalDog

私人_childCat作为MammalCat


但也许这就是我需要的每个

子类都有一个成员变量,就像使用composition(has-a)一样。


我得到继承背后的想法,但不知道如何实际实现它。


再次感谢您的帮助,


-Beth


" Family Tree Mike写道:
Hi, ftMike.
Thanks for your response.

I renamed the constructor in both classes, but it''s not working the way I
expected.

Now I have:
Friend Class ValidateValue
Private _child As ValidateValue
....
Friend Sub init(ByVal validate As String)
Dim compoundType As String
Dim remainder As String
Dim openParenPos As Integer
Dim commaPos As Integer

openParenPos = InStr(validate, "(")
If openParenPos 0 Then
compoundType = Mid(validate, 1, openParenPos - 1)
remainder = Mid(validate, openParenPos + 1)
remainder = Mid(remainder, 1, Len(remainder) - 1) '' strip closing paren
Select Case compoundType
Case "text", "textnum"
_child = New ValidateValueText
_child.inittext(compoundType, remainder) '' this doesn''t compile
....
Friend Class ValidateValueText
Inherits ValidateValue

Private _textMaxLen As Integer
Private _textMinLen As Integer
Private _validateType As validateTypes

Friend Sub initText(ByVal compoundType As String, ByVal remainder As String)
....

but the _child object in ValidateValue acts like a ValidateValue object in
intellisense (the way it was declared,) not a ValidateValueText object (the
way it was instantiated.)

So I have an ''is-a'' relationship, like ''a dog is a mammal,'' and I''m trying
to figure out how to pass a mammalID to the Mammal class, which, if it''s
determined to be a dog, then creates an instance of a Dog object made
available to the caller, along with setting the mammalType property to ''dog''.

What I have is roughly equivalent to:
Friend Class Mammal
Private _child As Mammal
....
Friend Sub init(ByVal mammalID As String)
Dim compoundType As String
Dim remainder As String
Dim openParenPos As Integer
Dim commaPos As Integer

openParenPos = InStr(mammalID, "(")
If openParenPos 0 Then
compoundType = Mid(mammalID, 1, openParenPos - 1)
remainder = Mid(mammalID, openParenPos + 1)
remainder = Mid(remainder, 1, Len(remainder) - 1) '' strip closing paren
Select Case compoundType
Case "dog", "wolf"
_child = New MammalDog
_child.initDog(compoundType, remainder)
....
Friend Class MammalDog
Inherits Mammal

Private _isWild as Boolean
Private _mammalType As mammalTypes

Friend Sub initDog(ByVal compoundType As String, ByVal remainder As String)
---
_child.initDog(compoundType, remainder) won''t compile, as it doesn''t see
initDog as a method of the _child object. It sees all the properties and
methods of Mammal.

What I''m trying to avoid is having something like:
Friend Class Mammal
Private _childDog As MammalDog
Private _childCat As MammalCat

but maybe that''s what I need to do- have a member variable for each
subclass, like with composition (has-a).

I get the idea behind inheritance, but not how to actually implement it.

Thanks again for the help,

-Beth

"Family Tree Mike" wrote:

嗯,有没有理由你不想要空构造函数

ValidateValue?你不一定要打电话。在那之后,似乎

就像你可能意味着_textMaxLen,_textMinLen和_validateType

受到保护,而非私人。


"贝丝" < Be ** @ discuss.microsoft.com写信息

news:05 ************************** ******** @ microsof t.com ...
Well, is there a reason you don''t want an empty constructor in
ValidateValue? You don''t necessarily have to call it. After that, it seems
like you probably meant to have _textMaxLen, _textMinLen and _validateType
be protected, not private.

"Beth" <Be**@discussions.microsoft.comwrote in message
news:05**********************************@microsof t.com...

你好。


我是试图找出如何创建具有特定属性

的子类到子类,到目前为止它并不顺利。


现在我有一个带有表示类型的枚举的类。班级

有所有类型的所有属性

,但我想要的是



将这些属性移动到特定于每种类型的子类。


我的代码如下:


朋友类ValidateValue

Private _textMaxLen As Integer

Private _textMinLen As Integer

Private _validateType as validateTypes


Friend Sub New(ByVal validate As String )

Dim compoundType As String

Dim remainder As String

Dim openParenPos As Integer

Dim commaPos As Integer


openParenPos = InStr(验证,"(")

如果openParenPos 0那么

compoundType = Mid(验证,1 ,openParenPos - 1)

余数=中(验证,openParenPos + 1)

余数=中(余数,1,Len(余数) - 1)''剥离结束paren

Select Case compoundType

Case&quo t; text"," textnum"

if compoundType =" text"然后

_validateType = validateTypes.text

否则

_validateType = validateTypes.textNum

结束如果

commaPos = InStr(余数,&,,)

如果commaPos = 0那么

_textMaxLen = CInt(余数)

其他

_textMaxLen = CInt(中(余数,1,逗号 - 1))

_textMinLen = CInt(中(余数,逗号+ 1))

结束如果


但由于maxLen和minLen属性特定于两个文本

类型,我想要做的是声明一个子类它继承自

现有类,并包含和设置给定compoundType和余数的validateType,textMaxLen和

textMinLen属性。


我遇到的问题是子类的构造函数。在

子类中,

我想传递compoundType和余数,并且代码如下:

''Friend Class ValidateText
''继承ValidateValue


''私有_textMaxLen为整数

''私有_textMinLen为整数

' 'private _validateType as validateTypes


''Friend Sub New(ByVal compoundType As String,ByVal remaining As String)

''Dim commaPos As Integer

''如果compoundType =" text"然后

''_ validateType = validateTypes.text

''否则

''_ validateType = validateTypes.textNum

''结束如果

''commaPos = InStr(余数,,)

''如果commaPos = 0那么

' '_textMaxLen = CInt(余数)

''否则

''_ textMaxLen = CInt(中(余数,1,逗号 - 1))

''_ textMinLen = CInt(中(余数,逗号+ 1))

''结束如果

''结束子


但是这不起作用,因为基类没有构造函数

没有参数。我想在一个基类中使用一个参数作为键



确定要创建的子类然后创建我的子类之一。

我在基类中思考我需要一个变量,如:

Private _child as ValidateValue


在基类中的构造函数,代码如下:

Select Case compoundType

Case" text"," textnum"

_child = new ValidateText(复合类型,余数)


但是我不能让子类工作。


我应该采用不同的方式做什么?


感谢您的帮助,


-Beth
Hello.

I''m trying to figure out how to create subclasses with properties specific
to the subclass and so far it isn''t going well.

Right now I have a class with an enum representing the type. The class
has
all the properties specific to all the types, but what I want instead is
to
move those properties to subclasses specific to each type.

I have code like this:

Friend Class ValidateValue
Private _textMaxLen As Integer
Private _textMinLen As Integer
Private _validateType As validateTypes

Friend Sub New(ByVal validate As String)
Dim compoundType As String
Dim remainder As String
Dim openParenPos As Integer
Dim commaPos As Integer

openParenPos = InStr(validate, "(")
If openParenPos 0 Then
compoundType = Mid(validate, 1, openParenPos - 1)
remainder = Mid(validate, openParenPos + 1)
remainder = Mid(remainder, 1, Len(remainder) - 1) '' strip closing paren
Select Case compoundType
Case "text", "textnum"
If compoundType = "text" Then
_validateType = validateTypes.text
Else
_validateType = validateTypes.textNum
End If
commaPos = InStr(remainder, ",")
If commaPos = 0 Then
_textMaxLen = CInt(remainder)
Else
_textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
_textMinLen = CInt(Mid(remainder, commaPos + 1))
End If

But since the maxLen and minLen properties are specific to the two text
types, what I want to do is declare a subclass which inherits from the
existing class and contains and sets the validateType, textMaxLen, and
textMinLen properties given the compoundType and remainder.

The problem I''m having is with the subclass''s constructor. In the
subclass,
I want to pass the compoundType and remainder, and have code like:
''Friend Class ValidateText
'' Inherits ValidateValue

'' Private _textMaxLen As Integer
'' Private _textMinLen As Integer
'' Private _validateType As validateTypes

'' Friend Sub New(ByVal compoundType As String, ByVal remainder As String)
'' Dim commaPos As Integer
'' If compoundType = "text" Then
'' _validateType = validateTypes.text
'' Else
'' _validateType = validateTypes.textNum
'' End If
'' commaPos = InStr(remainder, ",")
'' If commaPos = 0 Then
'' _textMaxLen = CInt(remainder)
'' Else
'' _textMaxLen = CInt(Mid(remainder, 1, commaPos - 1))
'' _textMinLen = CInt(Mid(remainder, commaPos + 1))
'' End If
'' End Sub

But that doesn''t work because the base class doesn''t have a constructor
without parameters. I want to use a parameter into a base class as a key
to
determine the subclass to create and then create one of my subclasses.

I''m thinking in the base class I need a variable like:
Private _child as ValidateValue

And in the base class''s constructor, have code like:
Select Case compoundType
Case "text", "textnum"
_child = new ValidateText(compoundType, remainder)

But I can''t get the subclass to work.

What should I be doing differently?

Thanks for any help,

-Beth


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

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