Skip to content

Instantly share code, notes, and snippets.

@andyyou
Created April 7, 2013 04:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyyou/5329077 to your computer and use it in GitHub Desktop.
Save andyyou/5329077 to your computer and use it in GitHub Desktop.
about virtual and override confuse
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Obj5
{
class Program
{
static void Main(string[] args)
{
Car c = new Car();
c.Brand = "BMW";
c.Name = "Z4";
// 轉型之後為何拿的到 Brand
// Readme: http://msdn.microsoft.com/zh-tw/library/ms173153%28v=vs.80%29.aspx
((Vehicle)c).Run();
Console.ReadKey();
}
}
public class Vehicle
{
private string name;
public string Name { get; set; }
public virtual void Run()
{
Console.WriteLine(this.Name + " Go!");
}
}
public class Car : Vehicle
{
private string brand;
public string Brand { get; set; }
public override void Run()
{
Console.WriteLine(this.Brand + "Go Go ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment