using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Chinese c = new Chinese();
c.SayHello();
Japanese j = new Japanese();
j.SayHello();
Console.ReadKey();
}
}
public class Person
{
public virtual void SayHello()
{
Console.WriteLine("Hello");
}
}
public class Chinese : Person
{
public override void SayHello()
{
Console.WriteLine("你好");
// base.SayHello();
}
}
public class Japanese : Person
{
//这个表示类中的一个全新的方法SayHello,为什么可以添加一个和父类中的SayHello一模一样的方法呢?
//因为使用了new关键字,将从父类继承下来的SayHello方法隐藏掉了
public new void SayHello(){
Console.WriteLine("新方法");
base.SayHello();
}
}
}
C#虚方法
最新推荐文章于 2023-04-06 17:09:44 发布