本文介绍了子类如何访问父变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好, 有没有人知道如何在类中声明一个变量,只能从该类中实例化的类中访问? For例如: ************* CODE ***************** 公共班级家长 ''***如何宣布*** Dim Age As String =" 1/1 / 2000" ; Public Sub New() 昏暗的孩子作为新生儿() 结束子 结束班 公共班儿童 Public Sub GetParentAge() ''***要投入什么这里代替MyParent *** MsgBox(MyParent.Age) 结束分 结束课 ***************************************** /> TIA Goran Djuranovic 解决方案 声明为Private的变量就是私有。 声明为Friend的变量可以被子类引用。 声明为Public的变量可供所有人使用。 除非Child是Parent的子类,否则Friend变量不是可用。 我能做的一件事建议,如果孩子确实需要查看私人 或父母的朋友变量,请将其保存在私人收藏中, ,其中键值为变量,并将那个 集合byRef传递给Child类的构造函数,然后可以 持有refe来自收藏。 Tom 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 SubEnd ClassPublic Class ChildPublic Sub GetParentAge()''*** WHAT TO PUT HERE instead of MyParent***MsgBox(MyParent.Age)End SubEnd Class*****************************************TIAGoran 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 notavailable.One thing I could suggest, if the Child really needs to see the Privateor Friend variables of the Parent, keep them in a Private collection,with the key value being the name of the variable, and pass thatcollection byRef to the constructor of the Child class, which can thenhold a reference to the collection.Tom 这篇关于子类如何访问父变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-01 19:43