抽象类中的静态属性

发布于 2024-09-25 09:00:12 字数 337 浏览 3 评论 0原文

有人能解释为什么静态属性为空吗?

class Program
{
    static void Main(string[] args)
    {
        string s = Cc.P1; // is null
    }
}

public class Cc
    : Ca
{
    static Cc()
    {
        P1 = "Test";
    }
}

public abstract class Ca
{
    public static string P1
    {
        get;
        protected set;
    }
}

Could anybody explain why the static property is null?

class Program
{
    static void Main(string[] args)
    {
        string s = Cc.P1; // is null
    }
}

public class Cc
    : Ca
{
    static Cc()
    {
        P1 = "Test";
    }
}

public abstract class Ca
{
    public static string P1
    {
        get;
        protected set;
    }
}

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

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

发布评论

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

评论(4

眼趣 2024-10-02 09:00:12

这是因为当您编写 Cc.P1 时,您实际上指的是 Ca.P1,因为那是它的声明位置(因为 P1 是静态的) ,它不涉及多态性)。因此,不管表面如何,您的代码根本没有使用 Cc 类,并且 Cc 静态构造函数也不会执行。

That because when you write Cc.P1, you're actually referring to Ca.P1 because that's where it is declared (since P1 is static, it is not involved in polymorphism). So in spite of appearances, your code isn't using the Cc class at all, and the Cc static constructor is not executed.

美人骨 2024-10-02 09:00:12

尝试以下操作:

string s = Cc.P1; // is null
Cc c = new Cc();
s = Cc.P1; // is it still null

如果 P1 不再为 null,这是因为访问静态 P1(在 Ca 中)不会导致 Cc 的静态实例触发(因此会在静态构造函数中分配值)。

Try the following:

string s = Cc.P1; // is null
Cc c = new Cc();
s = Cc.P1; // is it still null

If P1 is no longer null, it is because accessing the static P1 (in Ca) does not cause the static instance of Cc to fire (and therefore assign the value in the static constructor).

聆听风音 2024-10-02 09:00:12

如果你真的想要这个值:

new Cc();  // call the static constructor
string s = Cc.P1; // not null now

If you really want the value:

new Cc();  // call the static constructor
string s = Cc.P1; // not null now
回首观望 2024-10-02 09:00:12

您在代码中滥用了一些 OOD 原则。例如,您在类中混合静态行为(巫术类似于单例设计模式)和多态性(您使用抽象基类但没有任何基类接口)。因为我们没有像“静态多态性”这样的东西,所以我们应该将这两个角色分开。

如果您更详细地描述您想要解决的问题,也许您会得到更准确的答案。

但无论如何你可以实现这样的东西:

public class Cc : Ca
{
    private Cc()
        : base("Test")
    {
       //We may call protected setter here
    }

    private static Ca instance = new Cc();
    public static Ca Instance
    {
        get { return instance; }
    }
}

public abstract class Ca
{
    protected Ca(string p1)
    {
        P1 = p1;
    }

    //You may use protected setter and call this setter in descendant constructor
    public string P1
    {
        get;
        private set;
    }
}


static void Main(string[] args)
{
    string s = Cc.Instance.P1; // is not null
}

You misuse some OOD principles in your code. For example, you mix in your classes static behavior (witch is something like Singleton design pattern) and polymorphism (you use abstract base class but without any base class interface). And because we have no such thing like "Static Polymorphism" we should separate this two roles.

If you describe in more details what problem are you trying to solve, maybe you receive more accurate answers.

But anyway you may implement something like this:

public class Cc : Ca
{
    private Cc()
        : base("Test")
    {
       //We may call protected setter here
    }

    private static Ca instance = new Cc();
    public static Ca Instance
    {
        get { return instance; }
    }
}

public abstract class Ca
{
    protected Ca(string p1)
    {
        P1 = p1;
    }

    //You may use protected setter and call this setter in descendant constructor
    public string P1
    {
        get;
        private set;
    }
}


static void Main(string[] args)
{
    string s = Cc.Instance.P1; // is not null
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文