示例#1
0
        public static void Main(string[] args)
        {
            House house = new House { Name="house"};
            Display(house);

            //UpCasting
            Stock msft = new Stock();
            Asset a = msft;
            if (a == msft)
                Console.WriteLine("Identical");

            Console.WriteLine(a.Name);
            //Console.WriteLine(a.SharesOwned); //Error

            //downcasting and as operator
            Asset asset = new Asset();
            //Stock stock = (Stock)asset; // this will fail at runtime
            Stock stock = asset as Stock; // this will not fail but return null and will not give actual casting error
            if (stock !=  null)
                Console.WriteLine(stock);
            else
                Console.WriteLine("stock object is null");

            //is operator
            if (asset is Stock)
                Console.WriteLine("a is stock");
            else
                Console.WriteLine("a is not stock");

            //virtual

            Console.Read();
        }
        private House House; //1、将可能需要的一些别的类的定义在自己的类中,

        #endregion Fields

        #region Methods

        //2、通过动态添加特定的实现了接口House的类,给OuStyle添加特定的实现了House接口的类,
        public void AddStyle(House house)
        {
            this.House = house;
        }
示例#3
0
        public static void Main(string[] args)
        {
            Stock stock = new Stock { Name = "hdfc", SharesOwned = 100 };
            House house = new House { Name = "bac", Mortgage = 100};

            Overrider overrider = new Overrider();
            BaseClass b = overrider;
            b.Foo();
            overrider.Foo();

            Hider hider = new Hider();
            BaseClass b2 = hider;
            hider.Foo();
            b2.Foo();

            Console.Read();
        }