C#第九次课笔记

116 阅读2分钟
    //namespace demo1011
    //{
    //    //class Animal   //定义了一个动物类
    //    //{
    //        //构造函数  初始化对象  当我们new创建一个对象的时候会执行构造函数 进行初始化  
    ////        public  Animal(string name1 ,int age1 ,int num2)
    ////        {
    ////            this.name = name1; //this指向当前创建的对象 
    ////            this.age = age1;
    ////            this.num1 = num2;
    ////        }

    ////        //字段
    ////        public string name;
    ////        public int age;
    ////        public int num1 = 1;

    ////        //属性
    ////        public  int  num
    ////        {
    ////            get
                {
    ////                Console.WriteLine("我是get,只要属性被读取就会执行");
    ////                return num1 ;//表示属性被访问的时候会执行改代码,同时返回一个值
    ////            }
    ////            set
    ////            {
    ////                num1 = value;
    ////                Console.WriteLine("新的值还没给num赋值",value);//set 表示当设置num值的时候会执行 value 表示新的值
    ////            }
    ////        }

    ////        //方法
    ////        public  void  Show ()
    ////        {
    ////            Console.WriteLine("我叫{0}会打篮球,今年{1}",name,age);
    ////        }
    ////    }
    ////    internal class Program
    ////    {
    ////        enum State
    ////        {
    ////            在线, 下线, 忙碌中, 隐身
            ////        }
    ////        struct Student
    ////        {
    ////            public string name;
    ////            public int id;
    ////            public int num;
    ////        }
    ////        delegate int GetNum(int num1, int num2);
    ////        static int Max(int num1, int num2)
    ////        {
    ////            return 1;
    ////        }
    //        static void Main(string[] args)
    //        {
    ////            Animal dog;
    ////            Animal cat;//声明一个dog对象
    ////            dog = new Animal("初一", 18, 10086);
    ////            cat = new Animal("初二", 3, 10088);
    ////            dog.Show();
    ////            cat.Show();




    ////            /*//枚举类型 
    ////            State state1;
    ////            state1 = State.忙碌中;
    ////            //遍历枚举类型中的名字
    ////            foreach (string name in Enum.GetNames(typeof(State)))
    ////            {
    ////                Console.WriteLine(name);
    ////            }
    ////            //遍历枚举类型中的值
    ////            foreach (int num in Enum.GetValues(typeof(State)))
    ////            {
    ////                Console.WriteLine(num);
    ////            }
    ////            // 结构体  多种数据类型组合  某一类信息 
    ////            Student student1;
    ////            Student student2;
    ////            Student student3;
    ////            student1.name = "小明";
    ////            student1.id = 12;
    ////            student1.num = 12;
    ////            student2.name = "小亮";
    ////            student2.id = 12;
    ////            student2.num = 12;
    ////            student3.name = "小明";
    ////            student3.id = 12;
    ////            student3.num = 12;

    ////            //委托 定义一个没有函数体的函数 和目标函数有相同的返回值和参数  使用委托指向目标函数
    ////            GetNum getNum;
    ////            getNum = Max;
    ////            getNum(10, 11);





    ////            Student[] student = new Student[3]{ student1,student1,student1}; 
    ////            foreach(Student i in student)
    ////            {
    ////                Console.WriteLine(i.name);
    ////                Console.WriteLine(i.id );
    ////                Console.WriteLine(i.num );
    ////            }*/

    ////            //类(方法,属性,字段等等) 增加代码的复用     对象是由类生成的   对象(具体) attack方法 
    ////            //创建 一个attack方法共有不就行了吗?  ----》》会存在什么问题   attack 用到对象的一些属性 或者调用了其他的方法



    //   }
    //   }
    //}
    using System;
   namespace demoche
   {
   //    //定义一个车辆类
   //   public class Vehicle
   //    {
           public   Vehicle (int Speed,int Maxspeed,int Weight)
   //        {
   //            this.Speed = Speed;
   //            this.Maxspeed = Maxspeed;
   //            this.Weight = Weight;
   //        }
   //       //字段
   //        public int Speed;
   //        public int Maxspeed;
   //        public int Weight;
   //        //方法
   //        public void Run()
   //        {
   //            Console.WriteLine("当前以{0}速度往前行驶,最大速度是{1}", Speed, Maxspeed);
   //        }
   //    }
   //    internal class Program
   //    {
   //        static void Main(string[] args)
   //        {
   //            //Vehicle Car;
   //            //Car = new Vehicle(10, 20, 90);
   //            //Car.Run();

   //            //匿名类型  var es6 let 
   //            //var a = "dfd"
   //            //var a = 1; //匿名类型的类型由赋值的值的类型决定
   

   //        }
   //    }
   //}
   
   
   
   继承
   namespace jicheng
  {
//创建一个基类

public class Enemy
{
    //public Enemy(int life)
    //{
    //    this.life = life;
    //}
    public int life;
    public virtual void move()//规定虚方法
    {
        Console.WriteLine("怪物移动"+life );
    }
}
//创建了一个派生类(子类)
class Boss:Enemy
{
    //虚方法(重写父类中的方法)  机制 调用 move方法的时候 如果子类中有move方法了就调用子类中的move方法
    public  override  void move ()
    {
        Console.WriteLine(1);
    }
}
internal class Program
{
   
    static void Main(string[] args)
    {
        Enemy enemy = new Enemy();
        enemy.move();
        Boss boss = new Boss();
        boss.move();
    }
}
}