Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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