西凉董卓 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 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示