访问基类中的子类...但不同

发布于 2024-12-08 20:52:08 字数 771 浏览 0 评论 0原文

我有一个基类,它有一个子类(我想可能是一个结构,但不确定它是否合适)和一个方法。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

十六岁半 2024-12-15 20:52:08

我不确定您是否真的需要子类化/类嵌套。只需移出类 SubClass 声明并声明 SubClass 类型的受保护字段/属性即可。

public class SubClass 
{
   public string word;
   public int number;
}

public class Base 
{
    protected SubClass subClassInstance = new SubClass();

    protected void SomeMethod()
    {
        this.subClassInstance.word //this is where I'm struggling
    }
}

然后您可以在 ChildClass1ChildClass2 中访问 subClassInstance

I'm not sure that you really need subclassing / class nesting. Just move out class SubClass declaration and declare protected field/property of SubClass type instead.

public class SubClass 
{
   public string word;
   public int number;
}

public class Base 
{
    protected SubClass subClassInstance = new SubClass();

    protected void SomeMethod()
    {
        this.subClassInstance.word //this is where I'm struggling
    }
}

Then you can access subClassInstance inside both ChildClass1 and ChildClass2

極樂鬼 2024-12-15 20:52:08

基类没有 SubClass 类型的字段或属性,因此您绝对不能直接执行您建议的操作。

一种解决方案是将字段添加

public childSubClass = new 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

public childSubClass = new SubClass();

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.

李不 2024-12-15 20:52:08

我不确定为什么你要创建一个子类,而不是仅仅创建基类的这两个属性,但是你在这行遇到麻烦的原因

this.SubClass.word //this is where I'm struggling 

是:因为你没有将子类实例化为基类的属性基类。

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 :

this.SubClass.word //this is where I'm struggling 

is because you're not instantiating SubClass as a property of the base class.

玩物 2024-12-15 20:52:08

基类不能(或不应该)访问派生类的成员,并且通常甚至不知道派生类(有一些例外,例如在状态模式)。如果基应该有权访问成员,则应该在基中声明它。如果派生类也应该能够使用该成员,则将该成员标记为受保护。

class Base
{
     protected Foo someFoo;

     void Frob()
     {
         // can access methods/properties of someFoo instance
     }
}

class Child 
{
    public Child() 
    {
        someFoo = new Foo(); // child can also access someFoo
    }
}

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.

class Base
{
     protected Foo someFoo;

     void Frob()
     {
         // can access methods/properties of someFoo instance
     }
}

class Child 
{
    public Child() 
    {
        someFoo = new Foo(); // child can also access someFoo
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文