英雄pk理解面向对象中的this指针概念

复制代码
class Hero
    {
        public int ATN { get; set; }
        public int DEF { get; set; }
        public int HP { get; set; }
        public string Name { get; set; }

        public bool Attack(Hero target)
        {
            bool hasDead = false;
            Random r = new Random();

            int damage = r.Next(this.ATN - target.DEF); 
            target.HP -= damage;
            Console.WriteLine("{0}向{1}发动攻击,造成{2}点伤害!", this.Name, target.Name, damage, target.HP);
            if (target.HP <= 0)
            {
                Console.WriteLine("{0}已经死亡!", target.Name);
                target.HP = 0;
                hasDead = true;
            }
            Console.WriteLine("{0}生命值变为{1}", target.Name, target.HP);
            return hasDead;
        }

        static void Main()
        {
            Hero A = new Hero() { Name = "卡特琳娜", ATN = 100, DEF = 50, HP = 300 },
                B = new Hero() { HP = 300, Name = "盖伦", ATN = 100, DEF = 50 };
            Console.WriteLine("---------------------\n      英雄联盟\n---------------------");
            while (A.HP >= 0 && B.HP >= 0)
            {
                if (B.Attack(A))
                {
                    Console.WriteLine("{0}获取胜利", B.Name);
                    break;
                }
                System.Threading.Thread.Sleep(500);
                if (A.Attack(B))
                {
                    Console.WriteLine("{0}获取胜利", A.Name);
                    break;
                }
                System.Threading.Thread.Sleep(500);
            }
            Console.WriteLine("请按任意键继续。。。");
            Console.ReadKey();
        }
    }
复制代码

 

posted @   际为软件事务所  阅读(341)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示