Created
April 7, 2013 04:48
-
-
Save andyyou/5329077 to your computer and use it in GitHub Desktop.
about virtual and override confuse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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