namespace LearnCSharp
{
class L_virtual
{
public void main()
{
Virtual_b b = new Virtual_b();
Virtual_a a = b;
a.show();
}
}
class Virtual_a
{
public virtual void show() //声明为虚方法
{
Log.d("virtual_a show");
}
}
class Virtual_b : Virtual_a
{
public override void show()//override重写基类方法
{
Log.d("virtual_b show");
}
}
}
##输出:virtual_b show
隐藏
namespace LearnCSharp
{
class L_virtual
{
public void main()
{
Virtual_b b = new Virtual_b();
Virtual_a a = b;
a.show();
}
}
class Virtual_a
{
public void show() // 没有virtual声明
{
Log.d("virtual_a show");
}
}
class Virtual_b : Virtual_a
{
public new void show()// 使用new隐藏父类方法
{
Log.d("virtual_b show");
}
}
}
##输出:virtual_a show