新手愚笨,这是什么问题啊!
using System;using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication19
{
class kaka
{
private int x;
private int y;
public int X
{
get { return x;}
set { x = value; }
}
public int Y
{
get { return y;}
set { y = value; }
}
public virtual int add1()
{
return X + Y;
}
}
class caca : kaka
{
public voerride int add1()
{
return X+Y+X;
}
}
class Program
{
static void Main(string[] args)
{
caca aa=new caca();
kaka bb = (kaka)aa;
aa.X = 3;
aa.Y = 3;
Console.WriteLine(bb.add1());
Console.WriteLine(aa.add1());
Console.ReadLine();
}
}
}
这个运行结果是9,9 而如果我把override替换为new则是6,9.
为什么呢?书上说这样效果是一样的呀?