默认计划
2389人加入学习
(4人评价)
【唐老狮】Unity基础课程之C#基础
价格 免费
承诺服务
该课程属于 Unity3d实战就业路线课程套餐
请加入后再学习

数组声明里存储的东西可以是结构体的构造方法

DoSomething[] array[i] = new DoSomething[10]

方法的参数也可以声明为结构体的使用,而且同样适用于ref和out

void DoSomething(ref DoAnything anything)

{

}

 

      public Monsters(string name)
        {
            this.name = name;
            Random randomize = new Random();
            this.atk = randomize.Next(5, 20);
        }

        public void StorgeMonsters()
        {
            Monsters[] monster = new Monsters[10];
            for (int i = 0; i < monster.Length; i++)
            {
                monster[i] = new Monsters("小怪兽" + i);
                monster[i].Atk();
            }
        }

        public void Atk()
        {
            Console.WriteLine("{0}的攻击力为{1}", name, atk);
        }

    struct OutMan
    {
        public string name;
        public int atk;
        public int hp;
        public int def;

        public OutMan(string name, int atk, int hp)
        {
            this.name = name;
            this.atk = atk;
            this.hp = hp;
        }

        public void Atk(ref LittleMonsters boss)
        {
            boss.hp = this.atk - boss.def;
            Console.WriteLine("{0}攻击了{1}造成{2}点伤害,{1}剩余血量为{3}", this.name, boss.name, (this.atk - boss.def), boss.hp);
        }
 
    }

    struct LittleMonsters
    {
        public string name;
        public int atk;
        public int hp;
        public int def;

        public LittleMonsters(string name, int atk, int hp)
        {
            this.name = name;
            this.atk = atk;
            this.hp = hp;
        }
        public void Atk(ref OutMan outman)
        {
            outman.hp = this.atk - outman.def;
            Console.WriteLine("{0}攻击了{1}造成{2}点伤害,{1}剩余血量为{3}", this.name, outman.name, (this.atk - outman.def), outman.hp);
        }

    }

[展开全文]

   #region 练习题一
        //使用结构体描述学员的信息,姓名,性别,年龄,班级,专业
        //创建两个学员对象,并对其基本信息进行初始化并打印

        struct Student
        {
            public string name;
            public bool sex;
             
            public int age;
            public int clas;
            public string major;

            public Student(string name,bool sex, int age, int clas,string major)
            {
                this.name = name;
                this.sex = sex;
                this.age = age;
                this.clas = clas;
                this.major = major;
            }

            public void Spesk()
            {
                string str = sex ? "女" : "男";
                Console.WriteLine($"姓名:{name},性别:{str},年龄:{age},教室:{clas},专业:{major}");
            }
        }

        #endregion

        #region 练习题二
        //请简要描述private和public两个关建字的区别
        //访问修饰符 用来修饰变量和函数的
        //如果默认不写访问修饰符 则为private
        //private 私有的 被它修饰的变量和函数 只能在结构体内部使用 外部无法使用 
        //public 公共的 被它修饰的变量和函数 能在内外部都访问

        #endregion

        #region 练习题三
        //使用结构体藐视矩形的信息,长,宽;创建一个矩形,
        //对其长宽进行初始化,并打印矩形的长、宽、面积、周长等信息。

        struct Rect
        {
            public float w;
            public float h;
            public float area;
            public float perimeter;

            public Rect(int w,int h)
            {
                this.w = w;
                this.h = h;
                area = w * h;
                perimeter = 2 * (h + w);
            }

            public void WriteInfo()
            {
                Console.WriteLine($"该矩形长为{w},宽为{h},面积为{area},周长为{perimeter}");
            }
        }
        #endregion

        #region 练习题四
        //使用结构体描述玩家信息,玩家名字,玩家职业
        //请用户输入玩家姓名,选择玩家职业,最后打印玩家的攻击信息
        //职业:
        //战士(技能:冲锋)
        //猎人(技能:假死)
        //法师(技能:奥术冲击)

        //打印结果:猎人唐老师释放了假死
        struct PlayerInfo
        {
            public string name;
            public E_occupation occupation;

            public PlayerInfo(string name,E_occupation occupation)
            {
                this.name =name;
                this.occupation = occupation;
            }

            public void Atk()
            {
                switch (occupation)
                {
                    case E_occupation.Warrior:
                        Console.WriteLine($"战士{name}释放了冲锋");
                        break;
                    case E_occupation.Hunter:
                        Console.WriteLine($"猎人{name}释放了假死");
                        break;
                    case E_occupation.Master:
                        Console.WriteLine($"法师{name}释放了奥术飞弹");
                        break;
                    default:
                        break;
                }
            }
        }
        enum E_occupation
        {
            /// <summary>
            /// 战士
            /// </summary>
            Warrior,
            /// <summary>
            /// 猎人
            /// </summary>
            Hunter,
            /// <summary>
            /// 法师
            /// </summary>
            Master,
        }
        #endregion

        #region 练习题五
        //使用结构体描述小怪兽
        struct Monster
        {
            public string name;
            public int atk;

            public Monster(string name)
            {
                this.name=name;
                Random r = new Random();  
                atk = r.Next(10, 31);
            }

            public void Atk()
            {
                Console.WriteLine($"{name}的攻击力是{atk}");
            }
        }

        #endregion

        #region 练习题六
        //定义一个数组存储10个上面描述的小怪兽,每个小怪兽的名字为(小怪兽+数组下标)
        //举例:小怪兽0,最后打印10个小怪兽的名字+攻击力数值

        
        #endregion

        #region 练习题七
        //应用已学过的知识,实现奥特曼打小怪兽
        //提示:
        //结构体描述奥特曼与小怪兽
        //定义一个方法实现奥特曼攻击小怪兽
        //定义一个方法实现小怪兽攻击奥特曼

        struct Atm
        {
            public string name;
            public int atk;
            public int def;
            public int hp;

            public Atm(string name,int atk,int def,int hp)
            {
                this.name = name;
                this.atk = atk;
                this.def = def;
                this.hp = hp;
            }
            public void Atk(ref Boss monster)
            {
               monster.hp -= this.atk - monster.def;
                Console.WriteLine($"{name}攻击了{monster.name},造成了{atk - monster.def}伤害,{monster.hp}剩余血量");
            }
        }

        struct Boss
        {
            public string name;
            public int atk;
            public int def;
            public int hp;
            public Boss(string name, int atk, int def, int hp)
            {
                this.name = name;
                this.atk = atk;
                this.def = def;
                this.hp = hp;
            }
            public void Atk(ref Atm atm)
            {
                atm.hp -= this.atk -atm.def;
                Console.WriteLine($"{name}攻击了{atm.name},造成了{atk - atm.def}伤害,{atm.hp}剩余血量");
            }
        }
        #endregion

        static void Main(string[] args)
        {
            Console.WriteLine("结构体练习题");

            //变量类型[] 数组名 = new 变量类型[10];
            //Monster[] monsters = new Monster[10];
            //for (int i = 0; i < monsters.Length; i++)
            //{
            //    monsters[i] = new Monster("小怪兽" + i);
            //    monsters[i].Atk();
            //}


            Atm a =new Atm("迪迦",10,5,100);
            Boss b = new Boss("lalla", 8, 4, 100);

            while (true)
            {
                a.Atk(ref b);
                if (b.hp <= 0)
                {
                    Console.WriteLine("奥特曼胜利");
                    break;
                }
                b.Atk(ref a);
                if (a.hp <= 0)
                {
                    Console.WriteLine("怪兽胜利");
                    break;
                }
                Console.WriteLine("按任意键继续");
                Console.ReadKey(true);
            }


            //Student s1 = new Student("1",false,18,1,"计算机");
            //Student s2 = new Student("2",true,18,2,"美术");
            //s1.Spesk();
            //s2.Spesk();

            //Rect rect = new Rect(10,10);
            //rect.WriteInfo();

            //Console.WriteLine("请输入你的姓名");
            //string name = Console.ReadLine();
            //Console.WriteLine("请输入职业");
            //try
            //{
            //    E_occupation o =(E_occupation)int.Parse(Console.ReadLine());
            //    //根据输入的内容初始化了一个玩家对象
            //    PlayerInfo info = new PlayerInfo(name,o);
            //    info.Atk();
            //}
            //catch 
            //{

            //    Console.WriteLine("请输入数字");
            //}
        }

[展开全文]

如果构造函数参数与内部申明变量不重名,则可以省略this进行赋值

[展开全文]

授课教师

游戏开发前端主程

课程特色

视频(42)
下载资料(37)

学员动态

从零开始学unity 加入学习
DCMax 加入学习
鱼排fish 加入学习
星星屑屑诶嘿嘿 加入学习
haixiao_stack 加入学习