用C#语言设计一个程序实现类的继承与多态,要求实现两种不同的方法重载?

代吗少点
2024-12-26 14:21:09
推荐回答(3个)
回答1:

using System;

public class A {

public virtual void Display () {

Console.WriteLine("运行时类型:" + this.GetType().ToString());
Console.WriteLine("这是A类的成员函数BaseDisplay().");

}

public virtual void Display (string content) {

Console.WriteLine("运行时类型:" + this.GetType().ToString());
Console.WriteLine("这是A类的成员函数BaseDisplay()的重载.字符串内容为:" + content);

}

}

public class B : A {

public override void Display () {

Console.WriteLine("运行时类型:" + this.GetType().ToString());
Console.WriteLine("这是B类的成员函数Display().");

}

public override void Display (string content) {

Console.WriteLine("运行时类型:" + this.GetType().ToString());
Console.WriteLine("这是B类的成员函数Display()的重载.字符串内容为:" + content);

}

public static void Main (string [] args) {

A a = new A();
B b = new B();
a.Display();
a.Display("My Name Is a.");
b.Display();
b.Display("My Name Is b.");
A newA = b;
newA.Display();

}

}

回答2:

如此简单的问题还要问啊?

回答3:

这个我有,明天发上来