记录c#学习笔记
第七章:类成员
实验8-1:
实验要求:
1. 定义2个类,玩家类(Player)、枪类(Gun)。
2. 枪类使用静态成员实现2种枪开枪功能,一种枪为“biu~biu~”,一
种枪为“boom~boom~”。
3. 玩家类都为实例成员,具有2把枪的开枪功能的方法(调用枪类的静
态方法);具有前进功能“gogogo!”;具有后退功能“跑啊!”。
4. 在Main函数中实例化玩家类,并循环判断用户输入数字进行操作。
前进:1,后退:2,开第一把枪:3,开第二把枪:4,退出:0。
实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace qiang
{
class Player
{
public static void Theone()
{
Console.WriteLine("gogogogo");
}
public static void Thetwo()
{
Console.WriteLine("跑啊!");
}
}
class Gun
{
public static void shoot()
{
Console.WriteLine("biu---biu--biu-biu");
}
public static void boom(){
Console.WriteLine("boom-boom-boom");
}
}
class Interface{
static void Main(string[] args)
{
Console.WriteLine("欢迎来到游戏界面");
Console.WriteLine("前进:1,后退:2,开一枪:3,开第二枪:4,退出:0");
while (true)
{
Console.WriteLine("请输入操作命令:");
switch (Console.ReadLine())
{
case "1":
Player.Theone();
break;
case"2":
Player.Thetwo();
break;
case"3":
Gun.shoot();
break;
case"4":
Gun.boom();
break;
case "0":
Console.WriteLine("关闭游戏:");
Console.ReadLine();
return;
default:
Console.WriteLine("无效操作,请重新输入:");
break;
}
}
}
}
}
实现效果如图所示: