您的位置:首页 > 编程语言 > C#

C# 继承与多态,相关关键字virtual、override、new

2016-03-30 10:55 471 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication26
{
class Program
{
static void Main(string[] args)
{
animal a = new animal();
a.name = "animal";
a.say();
cat c = new cat();
c.name = "cat";
c.say();
dog d = new dog();
d.name = "dog";
d.say();
sheep s = new sheep();
s.name = "sheep";
s.say();
animal a1 = new cat();
a1.say();
animal a2 = new dog();
a2.say();
animal a3 = new sheep();
a3.say();
}
}
class animal
{
public string name { get; set;}
public virtual void say()
{
Console.WriteLine("animal say");
}
}
class cat : animal
{
public override void say()
{
Console.WriteLine("cat say");
}
}
class dog : animal
{
public new void say()
{
Console.WriteLine("dog say");
}
}
class sheep : animal
{
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 继承 多态