1)创建一个类D,然后在D中声明一个string类型的名为Result的属性,扩充类可通过该属性写入值,并编写一个可以被重写的带int型参数的方法MyMethod,在该方法中将传递给该方法的整型值加10后的结果添加到Result属性中。
2)创建一个类E,使其继承类D,然后在该类中重写D中的MyMethod方法,将D中接收的整型值加50,并将结果添加到Result属性中。
3)在主函数中分别创建类D和类E的对象,分别调用其MyMethod方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
public class Program
{
string Result;
public virtual void MyMethod(int x)
{
int y;
y = x + 10;
Result = Result + y;
}
public class ProgramA:Program
{
public override void MyMethod(int x)
{
int y;
y = x + 50;
Result = Result + y;
}
}
static void Main(string[] args)
{
Program program = new Program();
ProgramA a = new ProgramA();
a.Result = "I am a student";
program.Result = "I an a student";
program.MyMethod(10);
a.MyMethod(10);
Console.WriteLine(program.Result);
Console.WriteLine(a.Result);
Console.ReadLine();
}
}
}