本文介绍了在父构造函数中实例化类变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 嗨伙计们(加尔斯!), 我有2个班级,TypesafeConstant和颜色。 "颜色"从TypesafeConstant继承 ,并且不添加任何新功能。所有颜色 是实例化一些类变量,它们是 " Color"的公共实例。 什么是TypesafeConstant确实是创建一个共享列表,并将每个 声明添加到该列表中。然后,我可以使用 字符串查询该列表(例如Red)以返回Color.Red。 现在,如果我取出继承,并且只有颜色。实例化 本身,一切正常,但继承Parse 函数错误带有NullReferenceException,这是由 引起的TypesafeConstant.list是一个空引用。 我有什么想法可以重新编写它以使其工作吗? 哦,只是要说,我想要使用这种技术很多,因此 继承 - 我不想每次都要重写整个 TypesafeConstant! ! 非常感谢, RB。 ************ *代码******************* 公共类TypesafeConstant 私人sShortType 受保护的共享列表为ArrayList 受保护的子新(ByVal sShortType为字符串) Me.sShortType = sShortType 如果列表什么都没有那么 list =新的ArrayList() 结束如果 list.Add(Me) End Sub Public ReadOnly物业价值()作为字符串 获取 返回sShortType 结束获取 结束物业 公共共享函数Parse(ByVal sValue As String)As TypesafeConstant Dim o As TypesafeConstant For Each o在list.ToArray(GetType(TypesafeConstant)) 如果o.Value.ToUpper = sValue.ToUpper那么 返回o 结束如果 下一页 结束功能 结束类 公共类颜色 继承TypesafeConstant 公共共享红色为颜色=新颜色(红色) 公共共享绿色颜色=新颜色(绿色) 公共共享BlueAs颜色=新颜色(蓝色) 私有子新(ByVal sShort As String,ByVal sLong As String) MyBase.New(sShort,sLong) End Sub 结束班Hi guys (and gals!),I''ve got 2 classes, "TypesafeConstant" and "Color". "Color" inheritsfrom "TypesafeConstant", and adds no new functionality. All "Color" doesis to instantiate some class variables which are public instances of"Color".What "TypesafeConstant" does is to create a shared list, and add everydeclaration of itself to that list. I can then query that list with astring (e.g. "Red") to return Color.Red.Now, if I take out the inheritance, and just have "Color" instantiatingitself, everything works fine, but with the inheritance the Parsefunction errors with a "NullReferenceException", which is caused byTypesafeConstant.list being a null reference.Any ideas how I could re-write this to make it work?Oh, and just to say, I want to use this technique a lot, hence theinheritance - I don''t want to have to re-write the entireTypesafeConstant each time!!Many Thanks,RB.************* CODE *******************Public Class TypesafeConstantPrivate sShortTypeProtected Shared list As ArrayListProtected Sub New(ByVal sShortType As String)Me.sShortType = sShortTypeIf list Is Nothing Thenlist = New ArrayList()End Iflist.Add(Me)End SubPublic ReadOnly Property Value() As StringGetReturn sShortTypeEnd GetEnd PropertyPublic Shared Function Parse(ByVal sValue As String) AsTypesafeConstantDim o As TypesafeConstantFor Each o In list.ToArray(GetType(TypesafeConstant))If o.Value.ToUpper = sValue.ToUpper ThenReturn oEnd IfNextEnd FunctionEnd ClassPublic Class ColorInherits TypesafeConstantPublic Shared Red As Color = New Color("Red")Public Shared GreenAs Color = New Color("Green")Public Shared BlueAs Color = New Color("Blue")Private Sub New(ByVal sShort As String, ByVal sLong As String)MyBase.New(sShort, sLong)End SubEnd Class推荐答案 对不起,我的不好。我正在尝试简化新闻组的代码。 当然,Color的代码应该是读取 Private Sub New(ByVal sShort As String) MyBase.New(sShort) End Sub 干杯, RB。Sorry, my bad. I was trying to simplify the code for the newsgroup.The code for Color should, of course, readPrivate Sub New(ByVal sShort As String)MyBase.New(sShort)End SubCheers,RB. 对不起,我的不好。我正在尝试简化新闻组的代码。 当然,Color的代码应该是读取 Private Sub New(ByVal sShort As String) MyBase.New(sShort) End Sub 干杯, RB。Sorry, my bad. I was trying to simplify the code for the newsgroup.The code for Color should, of course, readPrivate Sub New(ByVal sShort As String) MyBase.New(sShort)End SubCheers,RB. 这篇关于在父构造函数中实例化类变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-21 18:26