c#第七天

using System;

namespace 面向对象封装
{
    /// <summary>
    /// 自定义枚举类型:性别
    /// </summary>
    enum EGender
    {
        F,//女性
        M//男性
    }
    /// <summary>
    /// 自定义枚举类型:血型
    /// </summary>
    enum EBlood
    {
        A,
        B,
        AB,
        O,
        Other
    }
    //封装一个人类
    class Person//类名都是大写字母开头
    {
        //一 数据成员(没有加权限的默认都是私有权限,在外面不能调用,类的内部可以调用)
        #region this关键字
        //表示当前对象,只能在实例函数中使用
        //使用this关键字区分字段和形参的区别
        //实例函数中默认传入this,表示当前对象

        #endregion

        #region 常量
        //常量定义的时候就要赋值,不赋值会出错
        //常量表现像静态(通过类名访问),但是不能添加static关键字修饰
        public const  float PI = 3.14159f;
        public const string home = "地球";
        
        #endregion

        #region readonly只读
        //定义的时候赋值
        //在运行的时候,只能在构造函数中再次赋值
        //表现像const类型的字段,只读
        readonly string born = "";//出生地
        #endregion

        #region static静态使用
        //static关键字可以修饰数据成员,函数成员,自定义数据类型
        //没有static修饰的成员都是属于实例的,调用的时候通过实例打点调用
        //添加了static修饰的成员都是属于类的,调用的时候通过类名打点调用,所有对象共享
        static string country = "中国";//国籍
        
        //通过公有函数对私有数据成员进行访问
        public static void setCountry(string _country)//写权限的开放
        {
            country = _country;
            //静态函数中不能调用非静态成员
            //blood = EBlood.A;//静态函数中不能调用非静态的字段
            //setAge(19);//静态函数中不能调用非静态的函数

            
        }

        public static string getCountry()//读权限的开放
        {
            return country;
        }
        #endregion

        
        #region 实例字段
        private int age = 20;//年龄,定义字段的时候可以给默认值
        private string name;//姓名,显示指定为私有权限
        EGender gender;//性别,不加访问权限,默认就是私有权限
        EBlood blood;//血型
        public bool isDead = true;//是否死亡
        char zodiac;//十二生肖
        #endregion


        //二 函数成员(构造函数,属性,普通函数,索引器,事件)
        #region 构造函数
        /*
         构造函数特点:
           1、没有返回值,不写void,和普通函数有区别
           2、名字和类名一样
           3、构造函数可以重载(可以有多个构造函数)
           4、系统会为每一个类提供默认的构造函数,一旦定义了自己的构造函数,默认的构造函数就消失,需要显示实现,跟结构体不一样
           5、构造函数用来实例化对象,和new一起调用,用来初始化字段
           6、构造函数可以在其它构造函数中调用
         */
        /// <summary>
        /// 实例构造函数
        /// 每生成一个实例,选择一个实例构造函数执行
        /// </summary>
        public Person()
        {
            //默认构造函数,如果没有给字段赋值,类中的字段都有默认值
            //引用类型数据默认值:null
            //值类型数字型数据默认:0
            //布尔值默认:false

            born = "北京西城xxx";//readonly字段可以在构造函数中赋值
        }
        /// <summary>
        /// 实例构造函数
        /// 每生成一个实例,选择一个实例构造函数执行
        /// </summary>
        /// <param name="_name"></param>
        public Person(string _name,string born)
        {
            name = _name;
            age = 30;//类中的字段,如果没有赋值,会有默认值,定义的时候如果赋值了,以赋值的为默认值
            gender = EGender.M;
            blood = EBlood.AB;
            //country = "中国";

            this.born = born;
            //born = "北京西城xxx";//readonly字段可以在构造函数中赋值
        }

        /// <summary>
        /// 静态构造函数
        /// 1、只能有一个静态构造函数
        /// 2、没有任何修饰符,不要写public
        /// 3、静态构造函数在调用类中静态成员时调用
        /// 4、静态构造函数比所有的实例构造函数先执行
        /// 5、不管生成多少个实例,都只执行一次
        /// </summary>
        static Person()
        {
            Console.WriteLine("静态构造函数被调用");
        }

        #endregion
        #region 普通函数

        
        public override string ToString()
        {
            string msg = string.Format("姓名:{0};年龄:{1};国籍:{2};性别:{3};血型:{4};是否在世:{5};生肖:{6}", name, age, country, gender, blood, isDead, zodiac);
            return msg; 
        }
        //添加一个打印人类信息的函数
        public void print()
        {
            Console.WriteLine(ToString());//类内部,函数成员之间是可以相互访问的
           
        }

        public void setAge(int _age)
        {
            age = _age;

            //非静态函数成员可以调用静态成员
            country = "中国";//非静态函数成员可以调用静态字段
            setCountry("中国");//非静态函数成员可以调用静态函数

        }
        public int getAge()
        {
            return age;
        }
        public void setName(string name)
        {
            //使用this关键字区分字段和形参的区别
            //实例函数中默认传入this,表示当前对象
            this.name = name;
        }
        #endregion
    }
    class Program
    {
        static void Main(string[] args)
        {
            //一     生成类Person对象(对象所占内存空间只跟数据成员有关系),只需要调用其中一个构造函数即可
            //1.1   语法:类型名字 对象名=new 构造函数();
            Person xiaoming = new Person("xiao ming","北京西城");
            //Chinese.country = "中华人民共和国";//通过类名调用静态成员
            //1.2   修改类中的私有成员
            Person.setCountry("中华人民共和国");
            //1.3   访问类中私有成员
            string country=Person.getCountry();
            //1.4   在外部访问类中公有字段
            xiaoming.isDead = false;
            //1.5   调用对象公有函数(显示信息)
            xiaoming.print();

            //1.6   生成另一个对象
            Person zhangsan = new Person();
            zhangsan.setName("zhang san");
            zhangsan.setAge(30);
            zhangsan.print();


            //二 创建一个类的引用(别名),如果是空引用是不能去使用
            //语法:类型名 引用名=对象名;
            Person xiaoqiang = xiaoming;//xiaoqiang是xiaoming的别名,都是同一个对象
            xiaoqiang.print();


            //三 常量可以通过类名直接访问
            Console.WriteLine("PI=" + Person.PI);

            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 面向对象封装
{
    enum EQuality
    {
        White,
        Blue,
        Purple,
        Orange,
        Red,
        Gold
    }
    class ItemConfig
    {
        readonly int id;
        readonly string name;
        readonly EQuality quality;
        readonly float price;
        readonly string des;

        public ItemConfig(int id, string name, EQuality quality, float price, string des)
        {
            this.id = id;
            this.name = name;
            this.quality = quality;
            this.price = price;
            this.des = des;
        }
    }
}
using System;

namespace cOOP_Player
{
    enum WeaponType
    {
        Knife,    //匕首
        MachineGun,//机枪
        SniperRifle//狙击枪
    }

    class Weapon
    {
        public WeaponType weaponType;
        public string name;
    }
    
    class Player
    {
        public string name;
        public byte age;
        public float health;
        public Weapon[] weaponsBag;

        /// <summary>
        /// 当前正在使用的武器
        /// </summary>
        public Weapon currentWeapon;

        /// <summary>
        /// 攻击目标玩家
        /// </summary>
        /// <param name="attackTarget"></param>
        public void Attack(Player attackTarget)
        {
            if (attackTarget == null)
            {
                Console.WriteLine("攻击的目标为空!");
                return;
            }

            if (currentWeapon == null)
            {
                Console.WriteLine("您当前未持有武器,无法攻击");
                return;
            }

            Console.WriteLine($"{name}正在攻击{attackTarget.name}...");
            
            //造成的伤害
            float damage = 0;
            
            switch (currentWeapon.weaponType)
            {
                case WeaponType.Knife:
                    damage = 10;
                    break;
                case WeaponType.MachineGun:
                    damage = 30;
                    break;
                case WeaponType.SniperRifle:
                    damage = 50;
                    break;
            }
            
            //给对方造成伤害
            attackTarget.TakeDamage(damage);
            
        }

        /// <summary>
        /// 受到伤害
        /// </summary>
        public void TakeDamage(float damage)
        {
            health -= damage;

            Console.WriteLine($"{name}受到了{damage}点伤害");

            if (health <= 0)
            {
                Console.WriteLine($"{name}角色已经死亡...");
            }
            else
            {
                Console.WriteLine($"剩余血量{health}");
            }
        }

        /// <summary>
        /// 更换武器
        /// </summary>
        /// <param name="weaponIndex">武器编号【从0开始计数】</param>
        public void ChangeWeapon(int weaponIndex)
        {
            Console.WriteLine($"{name}正在更换武器...");

            if (weaponIndex < 0 || weaponIndex >= weaponsBag.Length)
            {
                Console.WriteLine("武器编号错误,没有识别到该武器");
            }
            else
            {
                //当前武器发生了变更
                currentWeapon = weaponsBag[weaponIndex];
                
                Console.WriteLine("更换武器为" + currentWeapon.weaponType);
            }
        }

        /// <summary>
        /// 自我介绍
        /// </summary>
        public void ShowMe()
        {
            Console.WriteLine($"我叫{name}" +
                              $"今年{age}岁" +
                              $"我目前血量{health}");
            Console.WriteLine($"我有{weaponsBag.Length}把武器");
            
            for (int i = 0; i < weaponsBag.Length; i++)
            {
                Console.WriteLine($"第{i+1}是{weaponsBag[i].weaponType}");
            }
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            Weapon knife = new Weapon();
            knife.weaponType = WeaponType.Knife;
            Weapon ak47 = new Weapon();
            ak47.weaponType = WeaponType.MachineGun;
            Weapon awm = new Weapon();
            awm.weaponType = WeaponType.SniperRifle;
            
            Player xiaoming = new Player();
            xiaoming.name = "小明";
            xiaoming.age = 19;
            xiaoming.health = 100;
            xiaoming.weaponsBag = new Weapon[] {knife, ak47, awm};
            
            Player xiaohong = new Player();
            xiaohong.name = "小红";
            xiaohong.health = 200;
            
            //自我介绍
            xiaoming.ShowMe();
            //更换武器
            //xiaoming.ChangeWeapon(2);

            while (true)
            {
                Console.WriteLine("-----------------------------");
                Console.WriteLine("请输入指令:");
                Console.WriteLine("1、对当前角色造成10点伤害");
                Console.WriteLine("2、对对方角色造成伤害");
                Console.WriteLine("3、切换武器");
                Console.WriteLine("0、退出");
                Console.WriteLine("-----------------------------");
                
                
                int order = int.Parse(Console.ReadLine());
                

                if (order == 1)
                {
                    xiaoming.TakeDamage(10);
                }
                else if(order == 2)
                {
                    xiaoming.Attack(xiaohong);
                }
                else if(order == 3)
                {
                    Console.Write("请输入切换武器的编号:");
                    //用户输入武器编号
                    int index = int.Parse(Console.ReadLine());
                    //切换武器
                    xiaoming.ChangeWeapon(index);
                }
                else if(order == 0)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("指令错误...");
                }
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值