[9-27上午]3.简述接口隔离原则

Posted on 2009-10-09 15:16  Relax Active  阅读(203)  评论(0)    收藏  举报

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interface
{
    class Program
    {
        public interface Ifire
        {
            void fire();        
            void yongbao();
        }
        public class Tank : Ifire
        {

            public void fire()
            {

                Console.WriteLine("我是坦克,我要开火");
            }
        }
        static void Main(string[] args)
        {
            Tank ta = new Tank();
            ta.fire();
        }
    }
}

 此编译不能通过
错误 1 “Interface.Program.Tank”不实现接口成员“Interface.Program.Ifire.yongbao()” F:\my control program\ConsoleApplication3\ConsoleApplication3\Program.cs 15 22 ConsoleApplication3

解释:因为接口还定义了yongbao方法,子类继承了接口,而子类没用yongbao这个方法,所以会报错。

博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3