VR引擎第九课 :类、匿名类型

74 阅读1分钟

namespace vr91011 { //类 /* class Animal //定义了一个动物类 { / //构造函数 初始化对象 当new创建一个对象的时候会执行构造函数 进行初始化 / public Animal(string name2,int age2,int num2) { //this指向当前创建的对象 this.name = name2; this.age = age2; this.num1 = num2;

     }
     //字段
     public string name;
     public int age;
     public int num1;
     //属性
     public int num
     {
         //get 表示属性被访问的时候会执行该代码,同时返回一个值
         get {
             Console.WriteLine("get,只要属性被读取就会调用执行");
             return num1;
         }
         //set 表示当设置值的时候会被调用执行(这里是设置num值的时候会执行)
         set { 
             num1= value;
             Console.WriteLine("set,新的值还没给num 赋值",value);
         }
     }
     //方法
     public void Show()
     {
         Console.WriteLine("我叫{0},会打篮球,今年{1},号码是{2}", name, age,num1);
     }

 }

/******************************************************************************* //车辆类实例

    public class Vehicle
    {
        public Vehicle(double speed1, double maxspeed1, string weight)
        {
            this.speed = speed1;
            this.maxspeed = maxspeed1;
            this.weight = weight;
        }
        double speed;
        double maxspeed;
        string weight;
        public double maxspeed2 
        {
            get { return this.maxspeed; }
            set { this.maxspeed = value; }
        }
        public void Run()
        {
            Console.WriteLine("速度{0},最大速度{1},重量{2}", speed, maxspeed, weight);
        }
        public void Stop()
        {
            Console.WriteLine("已经停止");
        }

    }
      internal class Program
    {

static void Main(string[] args) {

        //类(方法,属性,字段等)增加代码的复用  对象由类产生  对象(具体) attack方法
        //创建一个attack方法公用--会存在什么问题 attack用到对象的一些属性 或者调用了其他方法

        /* Animal dog;//声明对象
         dog= new Animal("new",6,666);//创建对象
         *//*dog.name = "YI";
         dog.age = 3; 
         Console.WriteLine(dog.age);*//*
         dog.Show();
         dog.num = 10;//执行set块
         Console.WriteLine(dog.num);//执行get块
         Animal cat;
         cat = new Animal("liang", 3, 333);
         cat.Show();*/

/********************************************************* Vehicle car1; car1 = new Vehicle(80, 120, "30t"); car1.Run(); car1.maxspeed2 = 240; Console.WriteLine(car1.maxspeed2); car1.Run(); /********************************************************************** //匿名类型 var es6 let //不确定数据类型 可以使用var声明变量,该变量的类型由初始化赋值决定 var a = "wd"; var b = 1; Console.WriteLine(a); Console.WriteLine(b);

        }
        }
        }