10、接口

接口

接口除了实现是用:,其余和Java中的使用基本一致。

定义与实现

using System;
namespace HelloWorldApplication
{
    interface IAnimal{
        string? sayName();
    }

    class Dog : IAnimal
    {
        public string? name;

        public string? sayName()
        {
            return this.name;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Dog dog = new Dog();
            dog.name = "wangcai";
            Console.WriteLine(dog.sayName());
        }
    }
}