虚方法

/*this,base调用构造函数
 多态 里氏替换原则 子类可以赋值给基类    基类可以把指向的对象转换为对应的子类 虚方法
 语句
 */
namespace ConsoleApplication2
{
    class Vehicle
    {
        //两个字段,属性一个构造方法
        string _brand;
        string _color;
        public string Brand
        {
            get { return _brand; }
        }
        public string Color
        {
            get { return _color; }
            set { _color = value; }
        }
        public Vehicle(string a, string b)
        {
            _brand = a;
            Color = b;
        }
        public virtual void run()
        {
            System.Console.WriteLine("我开跑了");
        }
    }
    class Truck : Vehicle
    {
        int _weight;
        public Truck(string a, string b, int c)
            : base(a, b)//调用基类的构造函数,把参数传过去
        {
            _weight = c;
        }
        public override  void run()
        {
            Console.WriteLine("拉货去了");
        }
        public int Weight
        {
            get
            {
                return _weight;
            }

        }//class
        class car : Vehicle
        {
            int _passenger;//成员默认private只能从类内部使用
            public car(string a, string b, int c)
                : base(a, b)//调用基类的构造函数,把参数传过去
            {
                _passenger = c;
            }
            public override void run()//重写后不能在调用基类run方法
            {
                base.run();//在子类的方法中调用基类的方法。
                Console.WriteLine("载客去了");
            }
            public int passenger
            {
                get
                {
                    return _passenger;
                }

            }//class
            class Program
            {
                static void Main(string[] args)
                {

                    Truck t = new Truck("东风", "绿色", 10);
                    t.run();
                    t.run();
                   car c = new car("法拉利", "绿色", 10);
                    c.run();
                    c.run();
                    Vehicle[] v = {new car("qq","红色",10),new Truck ("213","白色",6)};
                    car cre = (car)v[0];//转不成功报异常
                    Truck tre = (Truck)v[1];
                    Console.WriteLine(cre);
                    Console.WriteLine(tre);
                    //for (int i = 0; i < v.Length; i++)
                    //{
                    //    if (v[i] is Truck)//is检测是否转换成功 as类似强制转换 false时返null 不抛异常
                    //    {
                    //        Truck tru = (Truck)v[i];
                    //        tru.carry();
                    //    }
                    //    else
                    //    // car caris=(car)v[i];//嵌入的语句不能是声明或标记语句
                    //    {//语句块
                    //        car caris = (car)v[i];//表达式语句
                    //        caris.carry();
                    //    }

                    //}
                    for (int i = 0; i <v.Length; i++)//控制流语句(条件执行语句,循环语句)
                    {
                        ; //空语句
                        int a, b, e;
                        b = 1;
                        e = 3;
                        a = b + e;//嵌入语句执行动作或管理控制流的语句
                        Console.WriteLine("虚方法开始用了");
                        v[i].run();//静态方法不能用this,base 子类赋值给基类。
                       // 基类虚方法调用子类的方法
             
                        Console.WriteLine("虚方法结束用了");
                    }
                    Console.ReadKey ();
                }
            }//class Program
        }//namespace
    }
}
   

if(变量名 is 子类) {}

else if(变量名 is 子类) {}

else if(变量名 is 父类){} 父类的变量名is 父类 永远为true 放到最后判断 顺序重要

原文地址:https://www.cnblogs.com/ggg34674/p/2581180.html