//C#继承
//继承类
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp3
{
public class Father
{
int x = 1;
public virtual void Reply()
{
Console.WriteLine(“father reply”);
}
public virtual void Test()
{
Console.WriteLine(“father test”);
}
}
public class Child : Father
{
public override void Reply()
{
Console.WriteLine(“child reply”);
}
public override void Test()
{
base.Test();
Console.WriteLine(“child test”);
}
}
}
//main类
using System;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Father father = new Father();
father.Reply();
father.Test();
Father father1 = new Child();
father1.Reply();
father1.Test();
Child child = new Child();
child.Reply();
child.Test();
Console.ReadKey();
}
}
}
//运行结果

该博客展示了C#中继承的代码实现。定义了父类Father和子类Child,子类重写了父类的方法。在Main方法中创建了父类和子类的对象,并调用相应方法,最后给出运行结果,体现了C#继承的特性。

被折叠的 条评论
为什么被折叠?



