1 Star 0 Fork 0

夏至青空/关于CSharp常用代码

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
常用泛型数据结构类List 练习题 1.23 KB
Copy Edit Raw Blame History
using System;
namespace 测试_Lesson7_练习题
{
//一个Monster基类,Boss和Goblin类继承它。
//在怪物类的构造函数中,将其存储到一个怪物List中
//遍历列表可以让Boss和Goblin对象产生不同攻击
abstract class Monster
{
public static List<Monster> monsters = new List<Monster>();
public Monster()
{
monsters.Add(this);
}
public abstract void Atk();
}
class Boss: Monster
{
public override void Atk()
{
Console.WriteLine("Boss的攻击");
}
}
class Goblin: Monster
{
public override void Atk()
{
Console.WriteLine("哥布林的攻击");
}
}
class Program
{
static void Main(string[] args)
{
Boss b = new Boss();
Goblin g = new Goblin();
Boss b2 = new Boss();
Goblin g2 = new Goblin();
for(int i = 0; i < Monster.monsters.Count; i++) //类名.列表名.列表长度
{
Monster.monsters[i].Atk();
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/summer-solstice-blue-sky/about---csharp-common-code.git
git@gitee.com:summer-solstice-blue-sky/about---csharp-common-code.git
summer-solstice-blue-sky
about---csharp-common-code
关于CSharp常用代码
master

Search

0d507c66 1850385 C8b1a773 1850385