隐藏 , 声明的类型会影响调用的方法是父类还是子类
using System;
namespace HelloOOP
{
class Program
{
static void Main(string[] args)
{
Vehicle car = new Car();
car.Run();
Car car2 = new Car();
car2.Run();
new Car().Run();
}
}
class Vehicle
{
public void Run()
{
Console.WriteLine("Vehicle run");
}
}
class Car : Vehicle
{
public void Run()
{
Console.WriteLine("Car run");
}
}
}
重写 , 不受声明类型的影响 ,根据实例的类型调用方法
using System;
namespace HelloOOP
{
class Program
{
static void Main(string[] args)
{
Vehicle car = new Car();
car.Run();
Car car2 = new Car();
car2.Run();
}
}
class Vehicle
{
public virtual void Run()
{
Console.WriteLine("Vehicle run");
}
}
class Car : Vehicle
{
public override void Run()
{
Console.WriteLine("Car run");
}
}
}
|