西凉董卓 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显然就不行了。
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办