Example #1
0
        }//定义静态方法Test,参数为任意类,功能是判断传进来的类 is class A/class B or not

        static void Main(string[] args)
        {
            int k = (int)DateTime.Now.DayOfWeek;//获取代表星期几的返回值

            switch (k)
            {
            case (int)Car.LimitedNumbers.Sun: Console.WriteLine("今天是星期日,双号限行"); break;

            case (int)Car.LimitedNumbers.Mon: Console.WriteLine("今天是星期一,双号限行"); break;

            case (int)Car.LimitedNumbers.Tue: Console.WriteLine("今天是星期二,单号限行"); break;

            case (int)Car.LimitedNumbers.Wed: Console.WriteLine("今天是星期三,双号限行"); break;

            case (int)Car.LimitedNumbers.Thi: Console.WriteLine("今天是星期四,单号限行"); break;

            case (int)Car.LimitedNumbers.Fri: Console.WriteLine("今天是星期五,双号限行"); break;

            case (int)Car.LimitedNumbers.Sat: Console.WriteLine("今天是星期六,单号限行"); break;
            }
            Console.ReadLine();
            int m;

            int.TryParse(Console.ReadLine(), out m);//获取代表汽车车型的返回值
            switch (m)
            {
            case (int)Car.TypeOfCar.Mini: Console.WriteLine("车型:微型轿车"); break;

            case (int)Car.TypeOfCar.Small: Console.WriteLine("车型:小型轿车"); break;

            case (int)Car.TypeOfCar.Middle: Console.WriteLine("车型:中级轿车"); break;

            case (int)Car.TypeOfCar.Intermediate: Console.WriteLine("车型:中高级轿车"); break;

            case (int)Car.TypeOfCar.Limousine: Console.WriteLine("车型:高级轿车"); break;
            }
            Console.ReadLine();
            Car car = new Car("BMW");

            //创建实例对象car,名字为“宝马”
            Car[] cartype = new Car[1];
            //创建一维数组,存放了两个对象
            cartype[0] = car;
            //第一个对象——宝马
            cartype[1] = new Car("BenZ");
            //第二个对象——奔驰
            string[] price = new string[] { "high", "medium", "low" };
            //一维数组——价格“高”“中”“低”
            int[] performence = new int[] { 1, 2, 3 };
            //一维数组——性能“1”“2”“3”
            char[] quality = new char[] { 'A', 'B', 'C' };
            //一维数组——质量“A”“B”“C”
            string[,] rank = new string[3, 3] {
                { "high", "1", "A" }, { "medium", "2", "B" }, { "low", "3", "C" }
            };
            //多维数组——价格、性能、质量三个一维数组组合成的数组

            //委托——显示转换
            Car.TypeOfCar car_1 = 0;
            Car.TypeOfCar car_2 = (Car.TypeOfCar) 3;
            Console.WriteLine(car_1);
            Console.WriteLine(car_2);
            int car_1_value = (int)Car.TypeOfCar.Mini;

            Console.WriteLine(car_1_value);
            Console.ReadLine();


            //委托——隐式转换
            int  a = 100;
            long b = a;


            //委托——引用类型之间的转换
            Minicar minicar_01 = new Minicar("Ferraria");

            minicar_01.Name = "法拉利";
            Car car_01 = minicar_01; //隐式转换
            Car car_02 = new Car("Audi");

            car_02.Name = "奥迪";
            Minicar minicar_02 = (Minicar)car_02;//显示转换

            Console.WriteLine(minicar_02.Name);
            //Minicar minicar_03 = (Minicar)car_02; // 错误:转换失败!

            Minicar[] MC1 = new Minicar[] { minicar_01, minicar_02 };
            Car[]     CC1 = MC1;
            Minicar[] MC2 = (Minicar[])CC1;
            Console.ReadLine();


            //泛型类--stack<T>实例化
            MyList <int> list = new MyList <int>(); //使用泛型类MyList<T>,来创建一个整数表

            for (int q = 0; q < 10; q++)            //通过简单地改变参数的类型
            {
                list.AddHead(q);                    //以创建字符串或其他自定义类型的表
            }
            foreach (int p in list)
            {
                Console.WriteLine(p);
            }
            Console.WriteLine("Done");


            //操作符
            bool[] x = new bool[5];
            Console.WriteLine("请依次输入5位考官的评分,1通过,0不通过: ");
            x[0] = (Console.ReadLine() == "1");
            x[1] = (Console.ReadLine() == "1");
            x[2] = (Console.ReadLine() == "1");
            x[3] = (Console.ReadLine() == "1");
            x[4] = (Console.ReadLine() == "1");
            int  i      = 0;
            bool result = (x[i++]) && (x[i++] || x[i]) && (x[++i] || x[++i]);

            Console.WriteLine("考核结果为: {0}", result);
            Console.WriteLine("判断次数为: {0}", i);
            Console.ReadLine();

            //as操作符
            object[] objArray = new object[6];        //定义数组,该数组的长度为6,包含的数据类型为object
            objArray[0] = new ClassA();               //第1个元素被赋予类A的一个对象
            objArray[1] = new ClassB();               //第2个元素被赋予类B的一个对象
            objArray[2] = "hello";                    //第3个元素被赋予字符串
            objArray[3] = 123;                        //第4个元素被赋予整型数据
            objArray[4] = 123.4;                      //第5个元素被赋予浮点数
            objArray[5] = null;                       //第6个元素被赋予空

            for (int j = 0; j < objArray.Length; ++j) //遍历数组objArray
            {
                string s = objArray[j] as string;     //把数组元算转换为字符串
                Console.Write("{0}:", j);             //输出该元算
                if (s != null)                        //如果转换结果不为空
                {
                    Console.WriteLine("'" + s + "'"); //输出转换结果
                }
                else//否则
                {
                    Console.WriteLine("不是字符串");
                }
            }
            Console.ReadKey();



            //is操作符
            ClassA a1 = new ClassA(); //声明类A的对象a1
            ClassB b2 = new ClassB(); //声明类B的对象b1

            Test(a1);
            Test(b2);
            Test("a string");
            Console.ReadKey();
        } //主函数
Example #2
0
 public void Test(ClassB x)
 {
     Console.WriteLine(MyProperty + ": " + x.MyProperty);
 }
Example #3
0
 public ClassA(ClassB b)
 {
     _b = b;
 }