C#学习(三)——密闭类、接口

     密闭类:有些类不希望其他人通过继承来修改。

     如果一个基类方法不希望子类对其重写,就可以不声明为virtual。如果是某个派生类方法不希望子类对其重写,同时时override重写,就可以使用sealed机制。

     密闭方法:不希望其他人重写该方法。

     利用上一篇博客中的实例来进行演示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ExtendsClassTest
{
    public class Pet
    {
        protected string _name;
        public Pet(string name)
        {
            _name = name;
        }
        public void printName()
        {
            Console.WriteLine("Pet's name is " + name);
        }
        virtual public void Speak()
        {
            Console.WriteLine(name + " is speaking");
        }
    }
 
    public class Dog:Pet
    {
        public Dog(string name):base(name)
        {
            
        }
        new public void printName()//最好加new关键字,否则会出现警告
        {
            Console.WriteLine("宠物的名字是 " + name);
        }
        sealed override public void Speak()//使用sealed后,其派生类不能再进行重写
        {
            Console.WriteLine(name + " is speaking:" + "wangwang");
        }
    }

    public class hashiqi:Dog
    {
        public hashiqi(string name):base(name)
        {
        }
        /*
        override public void Speak()
        {
            Console.WriteLine(name + " is speaking:" + "wangwang");
        }
        这里如果在编译器上去掉注释的话,编译器会报错,并提示基类中此类事密封的,无法重写
        */
    }


    public class Cat:Pet
    {
        public Cat(string name):base(name)
        {
            
        }
        new public void printName()//最好加new关键字,否则会出现警告
        {
            Console.WriteLine("宠物的名字是 " + name);
        }
        sealed override public void Speak()
        {
            Console.WriteLine(name + " is speaking:" + "miaomiao);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Pet[] pets = new Pet[]{new Dog("jack"),new Cat("Tom")};
            for(int i = 0; i < pets.Length;i++)
            {
                pets[i].Speak();
            }
        }
    }
}

     接口:接口跟Java含义一样,关键字也是interface,现在来看看其的语法:

interface ICatchMice
{
    void CatchMice();//默认加public修饰,不能加任何访问修饰符,public都不能加,不能显示出来
}

     接口的实现语法如下:

Cat:CatchMice
{
    public void CatchMice()
    {
        ...
    }
}

以下是接口代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ExtendsClassTest
{
    public interface ICatchMice
    {
        void CatchMice();
    }

    public class Cat:ICatchMice
    {
        public void CatchMice()
        {
            Console.WriteLine("Catch mice");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Cat c = new Cat();
            ICatchMice catchM = c;
            catchM.CatchMice();
            c.CatchMice();
        }
    }
}

        静态类、静态方法同Java定义类似,不同之处可能在于语法结构的不同,静态类可以进行方法的扩展,即不对原有的类或者方法进行改变,通过静态类引用来实现,代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ExtendsClassTest
{
    public class Pet
    {
        protected string _name;
        public Pet(string name)
        {
            _name = name;
        }
        public void printName()
        {
            Console.WriteLine("Pet's name is " + name);
        }
        virtual public void Speak()
        {
            Console.WriteLine(name + " is speaking");
        }
    }
 
    public class Dog:Pet
    {
        public Dog(string name):base(name)
        {
            
        }
        new public void printName()//最好加new关键字,否则会出现警告
        {
            Console.WriteLine("宠物的名字是 " + name);
        }
        sealed override public void Speak()//使用sealed后,其派生类不能再进行重写
        {
            Console.WriteLine(name + " is speaking:" + "wangwang");
        }
    }

    public class hashiqi:Dog
    {
        public hashiqi(string name):base(name)
        {
        }
        /*
        override public void Speak()
        {
            Console.WriteLine(name + " is speaking:" + "wangwang");
        }
        这里如果在编译器上去掉注释的话,编译器会报错,并提示基类中此类事密封的,无法重写
        */
    }
    
    static class PetGuide
    {
        public static void HowToFeed(this Dog dog)
        {
            Console.WriteLine("吃骨头!");
        }
    }

    public class Cat:Pet
    {
        public Cat(string name):base(name)
        {
            
        }
        new public void printName()//最好加new关键字,否则会出现警告
        {
            Console.WriteLine("宠物的名字是 " + name);
        }
        sealed override public void Speak()
        {
            Console.WriteLine(name + " is speaking:" + "miaomiao);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Pet[] pets = new Pet[]{new Dog("jack"),new Cat("Tom")};
            for(int i = 0; i < pets.Length;i++)
            {
                pets[i].Speak();
            }
            Dog dog = new Dog("tony");
            dog.HowToFeed();
        }
    }
}

 

你可能感兴趣的:(C#)