子类如何访问父变量 [英] How does child class access parent's variables

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

问题描述

大家好,

有没有人知道如何在类中声明一个变量,只能从该类中实例化的类中访问?


For例如:


************* CODE *****************

公共班级家长


''***如何宣布***

Dim Age As String =" 1/1 / 2000" ;


Public Sub New()

昏暗的孩子作为新生儿()

结束子


结束班

公共班儿童


Public Sub GetParentAge()

''***要投入什么这里代替MyParent ***

MsgBox(MyParent.Age)

结束分


结束课

*****************************************
/>
TIA

Goran Djuranovic

解决方案

Goran Djuranovic写道:

大家好,
有没有人知道如何在一个类中声明一个变量才能访问
只能从该类中实例化的类?

例如:

************* CODE *****************
公共班级家长
< br'>''***如何去除它***
Dim Age As String =" 1/1/1000"

Public Sub New()
Dim作为新生儿的孩子()
结束子
<最终班级

公共班级孩子

Public Sub GetParentAge()
''***在这里放什么代替MyParent ***
MsgBox(MyParent.Age)
End Sub

结束课
********************** *******************

TIA
Goran Djuranovic




声明为Private的变量就是私有。

声明为Friend的变量可以被子类引用。

声明为Public的变量可供所有人使用。


除非Child是Parent的子类,否则Friend变量不是
可用。


我能做的一件事建议,如果孩子确实需要查看私人

或父母的朋友变量,请将其保存在私人收藏中,

,其中键值为变量,并将那个

集合byRef传递给Child类的构造函数,然后可以

持有refe来自收藏。


Tom


你好,Goran,


" ;私人"变量只能由类访问。 受保护的

变量可供类和任何派生的(即

继承)类访问。


干杯,

兰迪

Goran Djuranovic写道:

大家好,
有谁知道如何在一个变量中声明一个变量可以访问的类
只能从该类中实例化的类?

例如:

************* CODE *****************
公共班级家长

''***如何去除它***
昏暗的年龄As String =" 1/1 / 2000"

Public Sub New()
昏暗的孩子如新孩子()
End Sub

结束班级

公共班级孩子

Public Sub GetParentAge()
''***什么东西放在这里而不是MyParent ***
MsgBox( MyParent.Age)
End Sub

结束课
************************** ***************

TIA
Goran Djuranovic


你在这里遇到了很多问题。


1.在Parent的构造函数中你创建了一个一个Child对象的实例和



立即超出范围。


2.每次创建一个实例一个父对象,你自动创建

一个Child对象的实例。这从来没有照顾过这种情况

其中

父母没有孩子。


如果你改写它,那么你将能够实现您的要求

用于:


公共类父母


私有m_age为字符串

私人m_children作为ArrayList


Public Sub New()


m_age = String.Empty


m_children =新的ArrayList


结束子


Public Sub New(年龄为字符串)


m_age =年龄


结束子


公共财产年龄()为字符串


获取

返回m_age

结束获取


设置(值为字符串)

m_age = value

结束集


结束财产


Public Sub AddChild()


m_children.Add(新孩子(我))


结束子


Public ReadOnly Property Child( index As Integer)As Child


获取

返回CType(m_children(index),Child)

结束获取


结束物业


结束班级


公共班级儿童


私人m_parent作为家长


Public Sub New(父母为父母)


m_parent =父母


结束子

Public ReadOnly Property ParentAge()As String


获取

返回m_parent.Age

结束获取


结束物业


结束班级


然后您可以使用这些物品:


Dim _parent作为新父母


_parent.Age =" 1/1/1000"


_parent.AddChild()


Console.WriteLine(_parent.Child(0).ParentAge)


或:


Dim _parent作为新父母(1/1/1000)


_parent.AddChild()


Console.WriteLine(_parent.Child(0).ParentAge)

" Goran Djuranovic" <去************** @ newsgroups.nospam>在消息中写道

新闻:OL ************** @ TK2MSFTNGP04.phx.gbl ...

大家好,

有没有人知道如何在类中声明一个变量只能从该类中实例化的类中获取




For例如:


************* CODE *****************

公共班级家长


''***如何宣布***

Dim Age As String =" 1/1 / 2000" ;


Public Sub New()

昏暗的孩子作为新孩子()

结束子

结束班

公共班级孩子


Public Sub GetParentAge()

''***什么输入此处而不是MyParent ***

MsgBox(MyParent.Age)

结束次级


结束班级

*****************************************


TIA

Goran Djuranovic


Hi all,
Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

''*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
''*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic

解决方案

Goran Djuranovic wrote:

Hi all,
Does anyone know how to declare a variable in a class to be accessible
ONLY from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

''*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
''*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic



Variables declared as Private are just that, private.
Variables declared as Friend can be referenced by a sub class.
Variables declared as Public are available to everyone.

Unless Child is a sub-class of Parent, the Friend variables are not
available.

One thing I could suggest, if the Child really needs to see the Private
or Friend variables of the Parent, keep them in a Private collection,
with the key value being the name of the variable, and pass that
collection byRef to the constructor of the Child class, which can then
hold a reference to the collection.

Tom


Hello, Goran,

"Private" variables are only accessible to the class. "Protected"
variables are accessible to the class and to any derived (i.e.
Inherited) classes.

Cheers,
Randy
Goran Djuranovic wrote:

Hi all,
Does anyone know how to declare a variable in a class to be accessible
ONLY from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

''*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
''*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic



You''ve got a number of problems here.

1. In the constructor of Parent you create an instance of a Child object and
it
immediately goes out of scope.

2. Every time you create an instance of a parent object, you automatically
create
an instance of a Child object. This never takes care of the situation
where
the parent has no children.

If you rewrite it thus, then you will be able to achieve what you are asking
for:

Public Class Parent

Private m_age As String
Private m_children As ArrayList

Public Sub New()

m_age = String.Empty

m_children = New ArrayList

End Sub

Public Sub New(age As String)

m_age = age

End Sub

Public Property Age() As String

Get
Return m_age
End Get

Set(value As String)
m_age = value
End Set

End Property

Public Sub AddChild()

m_children.Add(New Child(Me))

End Sub

Public ReadOnly Property Child(index As Integer) As Child

Get
Return CType(m_children(index), Child)
End Get

End Property

End Class

Public Class Child

Private m_parent as Parent

Public Sub New(parent As Parent)

m_parent = parent

End Sub

Public ReadOnly Property ParentAge() As String

Get
Return m_parent.Age
End Get

End Property

End Class

Then you can use the objects thus:

Dim _parent As New Parent

_parent.Age = "1/1/2000"

_parent.AddChild()

Console.WriteLine(_parent.Child(0).ParentAge)

or:

Dim _parent As New Parent("1/1/2000")

_parent.AddChild()

Console.WriteLine(_parent.Child(0).ParentAge)
"Goran Djuranovic" <go**************@newsgroups.nospam> wrote in message
news:OL**************@TK2MSFTNGP04.phx.gbl...
Hi all,
Does anyone know how to declare a variable in a class to be accessible ONLY
from a classes instantiated within that class?

For example:

************* CODE *****************
Public Class Parent

''*** HOW TO DECLARE IT ***
Dim Age As String = "1/1/2000"

Public Sub New ()
Dim child As New Child()
End Sub

End Class
Public Class Child

Public Sub GetParentAge()
''*** WHAT TO PUT HERE instead of MyParent***
MsgBox(MyParent.Age)
End Sub

End Class
*****************************************

TIA
Goran Djuranovic


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

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