练习题1:

class Program
{
static void Main(string[] args)
{
//得到一个士兵
Team t = new Team();
//实现多态的第二因素
Role r = new Solider();
t.addMember(r);
}
}
class Role
{
public string Name {
get; set; }
public virtual int attack()
{
return 0;
}
}
class Magicer : Role
{
private int level;
public int Level
{
get {
return level; }
set
{
if (value >= 1 && value <= 10)
{
level = value;
}
level = value;
}
}
public override int attack()
{
return level * 5;
}
}
class Solider : Role {
public int Hurt {
get; set; }
public override

本文通过三道练习题深入探讨C#中的多态特性,包括抽象类、接口和重写等概念,帮助读者巩固理解并提升编程能力。

3034

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



