第二十三节 sealed 密封 虚方法virtual

View Code
案例1:sealed 密封

namespace e1
{
    //在一个类前定义一个sealed关键字意味着它就是一个密封类
    //密封类不能被继承

    sealed class Test {}
    
    //编译错误:无法从密封中派生
    //class TestA : Test
    //{ }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

————————————————————————————————————

案例2:密封方法不能重写

namespace e2
{

    abstract class A
    {
        public abstract void F();
    }
    class B : A 
    {
        //对一个重写方法定义了sealed关键字意味着它最后一次重写
        public sealed override void F()
        {
            throw new Exception("The method or operation is not implemented.");
        }
    }
    class C : B
    {
        //编译报错:密封方法不能够重写
        //public override void F()
        //{

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


——————————————————————————————————————————

案例3:虚方法virtual

namespace e3
{
    public enum Genders{Male,Female}
    class Person
    {
        protected string name;
        protected int age;
        protected Genders genders;

        public Person(string name,int age,Genders genders)
        {
            this.name = name;
            this.age = age;
            this.genders = genders;
        }
        //虚方法与抽象方法不一样,它有具体的实现
        //访问修饰符 Virtul 方法名(参数列表)
        //{
                //方法主题
        //}
        //一旦定义了虚方法,意味着我的子类可以使用overide关键字去重写它(当然不是必须的)
        public virtual void SayHi()
        {
            string sex = string.Empty;
            switch (this.genders)
            {
                case Genders.Male: sex = "先生"; break;
                case Genders.Female: sex = "女生"; break;
                default: sex = "外星人"; break;
            }
            Console.WriteLine("姓名{0}{1}年龄{2}",name,sex,age);
        }

    }
}

————————————————————————

namespace e3
{
    class Student:Person
    {
        private string hobby;
        private int popularity;

        public Student(string name, int age, Genders genders, string hobby, int popularity)
            : base(name, age, genders)
        {
            this.hobby = hobby;
            this.popularity = popularity;
        }
        //假设我们在子类中觉得默认的虚方法不能满足我们的需求,那么我们可以重写父类中的虚方法
        public override void SayHi()
        {
            Console.WriteLine("姓名:{0},性别:{1},年龄:{2},爱好:{3},受欢迎度:{4}", name, genders, age, hobby, popularity);
        }
    }
}

——————————————————————————————

namespace e3
{
    class Teacher:Person
    {
        public Teacher(string name, int age, Genders genders)
            : base(name, age, genders)
        { 
        }
    }
}

——————————————————————————————————

namespace e3
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> list = new List<Person>();

            list.Add(new Teacher("张三", 25, Genders.Female));
            list.Add(new Student("李四", 18, Genders.Male, "跳跳舞", 101));
            //如果在子类中重写了虚方法,那么将会调用重写的方法
            //否则不调用
            for (int i=0; i < list.Count; i++)
            {
                list[i].SayHi();
            }
            Console.ReadKey();
        }
    }
}

————————————————————————————————

案例4:new 关键字

namespace e4
{
    class Cat
    {
        public void Eat()
        {
            Console.WriteLine("猫在吃东西......");
        }
    }

    class GrafieldCat : Cat
    { 
        //如果我们希望在子类中对Eat方法,有自己的实现,我们可以使用new 关键字
        //在子类中隐藏父类的方法
        public new void Eat()
        {
            Console.WriteLine("咖啡猫在吃东西...........");        
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            //Cat ct = new Cat();
            //ct.Eat();
            GrafieldCat gfc = new GrafieldCat();
            gfc.Eat();
            Console.ReadKey();
        }
    }
}

——————————————————————————————

案例5:重写父类子类修饰符一致

namespace e5
{
    class A
    {
        public virtual void F() { }
        protected virtual void F2() { }
    }

    class B : A
    {
        /*在重写方法的时候要注意,父类中的方法的访问修饰符和子类中重写的父类的修饰符必须一致
         * 否则编译通不过*/

        //protected override void F() { }
        //public override void F2() { }

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

        }
    }
}
View Code
案例1:object

namespace e6
{
    class Person  { }  
    class Program
    {
        static void Main(string[] args)
        {
            //Object是.NET所以类的根类,无论是系统定义的类还是用户
            //自定义的类,最终都是从Object类派生
            Person p = new Person();
            //因为Object是任何类的父类,所以他可以包含任何类型的
            object o = new Person();
            o = new Program();
        }
    }
}

——————————————————————————————

案例2:重写Object类继承下来的ToString方法

namespace e7
{
    //重写Object类继承下来的ToString方法
    class Person
    {
        public override string ToString()
        {
            return "我是一个人";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person p=new Person();
            Console.WriteLine(p.ToString());
            //如果我们直接使用Console.WriteLine()输出对象名:p
            //实际上系统会隐身的给p加上一个ToString()方法
            //即:Console.WriteLine(p),相当于:Console.WriteLine(p.ToString);
            Console.WriteLine(p);
            Console.ReadKey();
        }
    }
}

————————————————————————

案例3

namespace e8
{
    class StoreHourse
    {
        public object[] items;
        private int count;
        public StoreHourse(int size)
        {
            items = new object[size];
            count = 0;
        }
        //往仓库添加
        public void Add(object obj)
        {
            if (count < items.Length)
            {
                items[count] = obj;
                count++;
            }
            else
            {
                Console.WriteLine("仓库已爆满........");
            }
        }
    }
 
    class Program
    {
        static void Main(string[] args)
        {
            StoreHourse sh = new StoreHourse(5);
            sh.Add(100);
            sh.Add("Good");
            sh.Add(1.2558525);
            sh.Add(new Program());
            sh.Add(true);
            //sh.Add(2.36);
            foreach (object obj in sh.items)
            {
                Console.WriteLine(obj);
            }
           
            Console.ReadKey();
        }
    }
}

————————————————————————————————————————————————

案例4:习题

namespace l1
{
    class Vertebrate
    {
        protected string name;
        public Vertebrate(string name )
        {
            this.name = name;
        }
        public virtual void SayHi()
        {
            Console.WriteLine("{0}:呼吸",name);
        }
    }
    class Mammal:Vertebrate 
    {
        public Mammal(string name)
            : base(name)
        { }

        public override void SayHi()
        {
            Console.WriteLine("{0}:肺呼吸",name);
        }
    }
    class Fish:Vertebrate
    {
        public Fish(string name)
            : base(name)
        { }
        public override void SayHi()
        {
            Console.WriteLine("{0}:腮呼吸",name);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            List<Vertebrate> list = new List<Vertebrate>();
            list.Add(new Vertebrate("脊椎动物"));
            list.Add(new Mammal("哺乳动物"));
            list.Add(new Fish("鱼类"));
           
            foreach (Vertebrate VB in list)
            {
                VB.SayHi();
            }
            Console.ReadKey();
        }
    }
}

————————————————————————————————————

案例5:

namespace l2
{
    class Vertebrate
    {
        public void Eat()
        {
            Console.WriteLine("进食");
        }
    }
    class Mammal : Vertebrate
    {
        public new void Eat()
        {
            Console.WriteLine("哺乳动物进食");
        }
    }
   
    class Program
    {
        static void Main(string[] args)
        {
            Vertebrate vb = new Vertebrate();
            vb.Eat();
            Mammal ml = new Mammal();
            ml.Eat();
            Console.ReadKey();
        }
    }
}
————————————————————————————————

案例6:

namespace l3
{
    class Mammal
    {
       protected string skin;
        public string Skin
        {
            set { skin = value; }
            get { return skin; }
        }
        public Mammal(string skin)
        {
            this.skin = skin;
        }
        public virtual void IHave()
        {
            Console.WriteLine("{0}",skin);
        }
    }
    class Human : Mammal
    {
        protected string limb;
        public string Limb
        {
            set { limb = value; }
            get { return limb; }
        }
        public Human(string skin, string limb)
            : base(skin)
        {
            this.limb = limb;
        }

        public override void IHave()
        {
            Console.WriteLine("{0}\n{1}", skin,limb);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("我有:");

          Human hm = new Human("皮肤和毛皮", "双手和双脚"); 
           hm.IHave();
           Console.ReadKey();     
        }
    }
}

——————————————————————————————————————

案例7:

namespace l4
{
    class Vertebrate
    {
        public Vertebrate()
        {
            Console.WriteLine("脊椎动物类的构造函数");
        }
        ~Vertebrate()
        {
            Console.WriteLine("脊椎动物类的析构函数");
        }
    }
    class Mammal : Vertebrate
    {
        public Mammal()
        {
            Console.WriteLine("哺乳动物类的构造函数");
        }
        ~Mammal()
        {
            Console.WriteLine("哺乳动物类的析构函数");
        }
    }

    class Human : Mammal
    { 
         public Human()
        {
            Console.WriteLine("人类的构造函数");
        }
        ~Human()
        {
            Console.WriteLine("人类的析构函数");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Human hm = new Human();
            Console.WriteLine("\n我是一个人类\n");
            //查看析构函数  按ctrl+F5
        }
    }
}

————————————————————————————————————————————

案例8:

namespace l5
{
    class Mamal
    {
        private int age;
        public int Age
        {
            set { age = value; }
            get { return age; }
        }
        public Mamal(int age)
        {
            this.age = age;
        }

        public virtual void Myself()
        { }
      
    }
    class Human:Mamal
    {
        private string name;
        public string Name 
        {
            set { name = value; }
            get { return name; }
        }
        public Human(int age,string name):base(age)
        {
            this.name = name;
        }
        public sealed override void Myself()
        {
            Console.WriteLine("姓名:{0},年龄:{1}",name,Age);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Human hm = new Human(27,"Jack");
            
            hm.Myself();            
            Console.ReadKey();
        }
    }
}
posted @ 2012-06-20 21:19  ComBat  阅读(205)  评论(0编辑  收藏  举报