访问基类中的子类...但不同
我有一个基类,它有一个子类(我想可能是一个结构,但不确定它是否合适)和一个方法。
class Base
{
protected class SubClass
{
public string word;
public int number;
}
protected void SomeMethod()
{
this.SubClass.word //this is where I'm struggling
}
}
然后我有几个子类来实现我的基类,实例化 Base.SubClass 并向实例化的类添加一些值。
class ChildClass1 : Base
{
public childSubClass = new SubClass();
public void DoSomethingRidiculous()
{
childSubClass.word = "WhoFarted";
}
}
class ChildClass2 : Base
{
public childSubClass = new SubClass();
public void DoSomethingRidiculous()
{
childSubClass.word = "ItStinks";
}
}
如果可能的话,我希望能够从基类中获取 SubClass.word 的值。我认为我实现自己想法的尝试可能是错误的。
I have a base class that has a subclass (could be a struct i suppose but not sure if it's appropriate) and a method.
class Base
{
protected class SubClass
{
public string word;
public int number;
}
protected void SomeMethod()
{
this.SubClass.word //this is where I'm struggling
}
}
Then i have a couple child classes that implement my baseClass, instantiate the Base.SubClass and add some values to the instantiated class.
class ChildClass1 : Base
{
public childSubClass = new SubClass();
public void DoSomethingRidiculous()
{
childSubClass.word = "WhoFarted";
}
}
class ChildClass2 : Base
{
public childSubClass = new SubClass();
public void DoSomethingRidiculous()
{
childSubClass.word = "ItStinks";
}
}
If possible, I would like to be able to get the value of SubClass.word from within the Base class. I think that my attempt at implementing my idea is probably wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不确定您是否真的需要子类化/类嵌套。只需移出类
SubClass
声明并声明SubClass
类型的受保护字段/属性即可。然后您可以在
ChildClass1
和ChildClass2
中访问subClassInstance
I'm not sure that you really need subclassing / class nesting. Just move out class
SubClass
declaration and declare protected field/property ofSubClass
type instead.Then you can access
subClassInstance
inside bothChildClass1
andChildClass2
基类没有
SubClass
类型的字段或属性,因此您绝对不能直接执行您建议的操作。一种解决方案是将字段添加
到类
Base
本身。这有问题吗?另一种解决方案是使用反射来获取该字段的值,假设您正在反射的对象确实具有这样的字段。这确实很牵强,虽然从技术上来说它可能允许你做你所建议的事情,但它的代码味道非常糟糕。
The base class has no field or property of type
SubClass
, so you definitely cannot do what you propose directly.One solution would be to add the field
to class
Base
itself. Is there a problem with this?The other solution would be to use reflection to get the value of the field, assuming that the object you are reflecting on does have such a field. This is really far-fetched and while it might technically allow you to do what you propose, it has a very bad code smell.
我不确定为什么你要创建一个子类,而不是仅仅创建基类的这两个属性,但是你在这行遇到麻烦的原因
是:因为你没有将子类实例化为基类的属性基类。
I'm not sure why you're making a Sub Class instead of just making those two properties of the base class, but the reason you're having trouble with this line :
is because you're not instantiating SubClass as a property of the base class.
基类不能(或不应该)访问派生类的成员,并且通常甚至不知道派生类(有一些例外,例如在状态模式)。如果基应该有权访问成员,则应该在基中声明它。如果派生类也应该能够使用该成员,则将该成员标记为受保护。
A base class can not (or should not) access members of derived classes, and usually not even know about derived classes (some exceptions apply, such as in the case of the State Pattern). If the base should have access to a member, it should be declared in the base. If derived classes should also be able to use that member, then mark the member as protected.