西凉董卓 2016-03-04 12:30 采纳率: 100%
浏览 1179

关于base的问题关于base的问题关于base的问题关于base的问题

namespace ReWriteMethodX
{
class People
{
public virtual string GetName(string NameStr)
{
return NameStr;
}
}
class Chinese:People
{
public override string GetName(string NameStr)
{
string cName = base.GetName(NameStr);
cName="我的名字是"+cName;
return cName;
}
}
class Forginer : People
{
public override string GetName(string NameStr)
{
string fName = base.GetName(NameStr);
fName = "My name is " + fName;
return fName;
}
}

class Program
{
    static void Main(string[] args)
    {
        string tmpName;
        Forginer F=new Forginer();
        Chinese C=new Chinese();
        Console.WriteLine("Please insert your Name:");
        tmpName = Console.ReadLine();

        Console.WriteLine(F.GetName(tmpName));
        Console.WriteLine(C.GetName(tmpName));
        Console.ReadLine();
    }
}

}
这段代码用到两处base
这段string cName = base.GetName(NameStr);
我在想为什么非得要用base把参数传给父类
我直接这样不就可以了吗?string cName = NameStr;
直接赋值不就可以了吗?最后的结果是一样的

  • 写回答

1条回答

  • threenewbee 2016-03-04 16:24
    关注
     这代码写得不好,说白了,只是演示下base怎么用,而且想出来的场景也没有代表性。
    一个更好的例子是
    class A
    {
        private int x;
            public virtual int getx() { return x; }
    }
    class B : A
    {
        public override int getx() { return base.getx(); }
    }
    
    这里不用base显然就不行了。
    
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!