首页 新闻 赞助 找找看

C#如何利用静态实现多态?

0
悬赏园豆:20 [已解决问题] 解决于 2014-06-24 12:47
public abstract class Parent
{
    public static string str = "Parent";
    public static string Show()
    {
        return str;
    }
}
public abstract class Child:Parent
{
    public new static string str = "Child";
}

public class Test
{
    string result = Child.Show();
    //result="Parent"
    //请问如何让这里的result输出 "Child"???
}
i迷倪的主页 i迷倪 | 初学一级 | 园豆:185
提问于:2014-06-12 18:09
< >
分享
最佳答案
0
    public abstract class Parent
    {
        public static string str = "Parent";
        public static string Show()
        {
            return str;
        }
    }
    public abstract class Child : Parent
    {
        public new static string str = "Child";
        public new static string Show()
        {
            return str;
        }
    }

    public class Test
    {
        string result = Child.Show();
        //result="Child"
    }
收获园豆:15
刘宏玺 | 专家六级 |园豆:14020 | 2014-06-13 10:45

感谢回答,可问题是不想重写父类中的方法,不然就不用继承父类了

i迷倪 | 园豆:185 (初学一级) | 2014-06-15 19:05
其他回答(2)
0

楼主为啥要用静态值实现动态的变化呢,这个值要么提取出来,要么使用属性,不知道楼主是不是有什么特殊的需求才这样做,这样明显不合理哈

收获园豆:5
幕三少 | 园豆:1384 (小虾三级) | 2014-06-15 14:09

呵呵!确实很不合理,主要是静态使用起来方便,便想试试利用静态字段、属性、方法能不能实现多态,现在看来是想多了~静态这玩意既不能虚拟也不能抽象,看来是不能多态的了

支持(0) 反对(0) i迷倪 | 园豆:185 (初学一级) | 2014-06-15 20:25
0

Mark一下

毋忆典藏 | 园豆:202 (菜鸟二级) | 2016-11-18 15:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册