我可以拥有一个基类,其中每个派生类都有自己的静态属性副本吗?

发布于 2024-10-18 16:17:01 字数 722 浏览 7 评论 0原文

我有类似下面的情况:

class Base
{
     public static int x;
     public int myMethod()
     {
          x += 5;
          return x;
     }

}

class DerivedA : Base
{
}

class DerivedB : Base
{
}

我正在尝试设置它,以便每个派生类都有自己的 x 静态实例,如果我执行如下操作:

 DerivedA.x = 5;
 DerivedB.x = 10;

那么当我运行时:

 DerivedA.myMethod(); //The result will be 10
 DerivedB.myMethod(); //The reusult will be 15

我可以执行类似的操作吗?如何设置派生类来实现此目的?谢谢你们。

编辑:基本上,我有一堆派生类,每个派生类都有该类独有的属性。它对于每个实例都没有变化,因此我认为应该是一个静态变量。此外,该属性是通过方法设置的,该方法对于每个类都是相同的。 我试图避免在每个派生类中复制并粘贴该属性和方法的逻辑。我认为最好以某种方式将该逻辑移至每个类所派生的基类。但是,我需要每个派生类都有自己的该属性的副本。 我不一定要这样做,如果你们有任何更好的做法建议,我将很高兴听到一些更好的做法建议..谢谢!

I have something like the following situation below:

class Base
{
     public static int x;
     public int myMethod()
     {
          x += 5;
          return x;
     }

}

class DerivedA : Base
{
}

class DerivedB : Base
{
}

I am trying to set this up so that each derived class has its own static instance of x, if I do something like this:

 DerivedA.x = 5;
 DerivedB.x = 10;

then when I run:

 DerivedA.myMethod(); //The result will be 10
 DerivedB.myMethod(); //The reusult will be 15

Can i do something like this? How can I setup the derived classes to achieve this? Thanks guys.

EDIT: Basically, I have a bunch of derived classes that each have a property unique to that class. It does not vary for each instance, and thus I believe should be a static variable. Also, that property is set by a method, that is the same for each of these classes.
I am trying to avoid having to copy and paste the logic for that property and method in each of these derived classes. I thought it best to somehow move that logic to the base class from which each of these classes are derived from. But, I need each derived class to have its own copy of that property.
I do not necessarily have to do it this way, and I will be happy to hear some better practices suggestions if you guys have any.. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

寄风 2024-10-25 16:17:02

您将需要重新定义并隐藏所有派生类型中的字段和方法。

示例:

class DerivedA : Base
{
  public new static int x;
  public new int myMethod()
  {
    x += 5;
    return x;
  }
}

注意:不要这样做。修复你的设计。

编辑:

实际上,我有类似的构造。我用一个抽象(如果您需要默认值,请使用virtual)属性来解决它,然后从基类中使用该属性:

public abstract class Base
{
   public abstract string Name { get; }

   public void Refresh()
   {
     //do something with Name
   }
}

public class DerivedA
{
  public override string Name { get { return "Overview"; } }
}

您应该能够根据您的用例调整它。当然,如果只有派生类能够看到该属性,您当然可以将其设置为受保护的。

You will need to redefine and hide the field and method in all derived types.

Example:

class DerivedA : Base
{
  public new static int x;
  public new int myMethod()
  {
    x += 5;
    return x;
  }
}

Note: don't do it this way. Fix your design.

Edit:

Actually, I have a similar construct. I solve it with an abstract (if you need a default value, use virtual) property which then gets used from the base class:

public abstract class Base
{
   public abstract string Name { get; }

   public void Refresh()
   {
     //do something with Name
   }
}

public class DerivedA
{
  public override string Name { get { return "Overview"; } }
}

You should be able to adjust that for your use case. You can of course make the property protected if only deriving classes should be able to see it.

瑾夏年华 2024-10-25 16:17:02

以 Type 作为键的静态字典应该可以。我的意思是避免逻辑重复和每个派生类型的不同值但在实例之间共享。

public class Base
{
    private static Dictionatry<Type,int> _values;

    public int MyMethod()
    {
        _values[this.GetType()]+=5;
        return _values[this.GetType()];
    }
}

Static dictionary with Type as keys should do. I mean avoid logic repetition and different values for each derived type but shared among instances.

public class Base
{
    private static Dictionatry<Type,int> _values;

    public int MyMethod()
    {
        _values[this.GetType()]+=5;
        return _values[this.GetType()];
    }
}
囍孤女 2024-10-25 16:17:02

我通常将子类特定的东西实现为抽象 get 属性

public class Base
{
    // You must pick one option below

    // if you have a default value in the base class
    public virtual int x { get { return 7; /* magic default value */} }

    // if you don't have a default value
    // if you choose this alternative you must also make the Base class abstract
    public abstract int x { get; }
}

public class DerivedA : Base
{
    public override int x { get { return 5; } }
}

public class DerivedB : Base
{
    public override int x { get { return 10; } }
}

I usually implement subclass specific stuff as an abstract get property

public class Base
{
    // You must pick one option below

    // if you have a default value in the base class
    public virtual int x { get { return 7; /* magic default value */} }

    // if you don't have a default value
    // if you choose this alternative you must also make the Base class abstract
    public abstract int x { get; }
}

public class DerivedA : Base
{
    public override int x { get { return 5; } }
}

public class DerivedB : Base
{
    public override int x { get { return 10; } }
}
赤濁 2024-10-25 16:17:01

嗯,是的,你可以,但它涉及到一些泛型的技巧。

如果您修复设计以便不需要该静态字段,或者至少不需要每个后代,那就更好了,但这里是这样的:

class Base<TDescendant>
    where TDescendant : Base
{
     public static int x;
     public int myMethod()
     {
          x += 5;
          return x;
     }

}

class DerivedA : Base<DerivedA>
{
}

class DerivedB : Base<DerivedB>
{
}

这依赖于这样一个事实:具有静态字段的泛型类型将获得这些静态字段的单独副本您调用它的每种类型的静态字段。

但是,如果您打算从 DerivedA 或 DerivedB 下降,就会变得很棘手,所以我不建议沿着这条路走。

Well, yes, you can, but it revolves around a bit of a trick with generics.

It is much better if you fix your design so that you don't need that static field, or at least not per descendant, but here goes:

class Base<TDescendant>
    where TDescendant : Base
{
     public static int x;
     public int myMethod()
     {
          x += 5;
          return x;
     }

}

class DerivedA : Base<DerivedA>
{
}

class DerivedB : Base<DerivedB>
{
}

This relies on the fact that a generic type with static fields will get a separate copy of those static fields for each type you invoke it with.

However, if you intend to descend from DerivedA or DerivedB, it gets tricky, so I wouldn't recommend going down this lane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文