zoukankan      html  css  js  c++  java
  • 接口的实现和继承

    实现过程必须在实现接口的类中完成

    类继承具有单根性,接口可多重继承

    父接口也成为该接口的显示基接口

    接口多重继承时,派生接口名与父接口用冒号隔开,多个父接口之间用逗号隔开

    接口的成员之间不能同名,继承的成员不用再声明,但接口可以定义与继承而来的成员同名的成员,这称为接口成员覆盖了继承而来的成员,这不会导致错误,但编译器会给出一个警告,关闭警告提示的方法时在成员声明前加上一个new关键字,但如果没有覆盖父接口中的成员,使用new关键字会导致编译器发出警报.

    interface Interface1
    	{
    		Void read();
    		//不同接口(不包含派生)中允许有同名的成员,如我在Interface2中定义read()也是可以的
    		String read()
    		(
    get;
    		Set;
    		//同一接口中的成员名不能重名,及时类型不同
    )
    }
    
    
    //接口可以多重继承,多重继承要用逗号隔开。父接口要与派生接口冒号隔开
    //如我Interface1接口中有个void read()成员
    Interface Interface3:Interface1,Interface2
    {
    	new Void read();
    	//如果派生接口中对显示基接口中的成员进行重新定义时,需要使用new来解除警报
    
    }
    

    任务实施实例

    老鹰、麻雀、鸵鸟、都是鸟类,根据三者的共性,提取出鸟类作为父类,并且具有各自的特点,老鹰吃小鸡,麻雀吃粮食,鸵鸟吃青草

    老鹰和麻雀都会飞,如何实现这个“飞”的功能

    开放封闭原则

    定义

    软件实体应该可以扩展,但是不可以修改

    特性

    对扩展是开放的

    对修改是封闭的,开放既可以操作,关闭既不可以操作

     

    1) 在父类中添加“飞”,但是鸵鸟不会飞,所以不能再父类中;

    2) 在会飞的子类中添加“飞”功能。完全可以,但是违背了开放封闭原则,下次我们新加一个天鹅,我们还要去代码去查看“飞功能是如何实现的”,然后在天鹅中添加“飞”方法,相同的功能。重复的代码。明显不合理,也不利于扩展

    假如我在新增一个“气球”,我继承父类合适吗?很显然不行,我们需要一种规则,这种规则就是接口

    3) 老鹰和麻雀都会飞就实现了“飞”这个接口,鸵鸟不会飞,就不实现这个接口。

     

    例子:利用接口实现老鹰和麻雀会飞的方法

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口继承与实施
    {
        class tuon:niaol,Iflyable
        {
            public void fly()
            {
                Console.WriteLine("我是鸵鸟,我回跑");
             }
            public override void write()
            {
                Console.WriteLine("我是鸵鸟,我吃青草");
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口继承与实施
    {
        class laoy:niaol,Iflyable
        {
            //接口实现过程需要在实现接口的类中进行实现
            public void fly()
        {
            Console.WriteLine("我是老鹰,我会飞");
        }
            public override void write()
            {
                Console.WriteLine( "我是老鹰,我吃小鸡");
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口继承与实施
    {
        abstract class niaol
        {
            public abstract void write();
           
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口继承与实施
    {
        class Program
        {
            static void Main(string[] args)
            {   
                //声明一个接口数组flys  用来保存老鹰和鸵鸟类的接口方法
                Iflyable[] flys = { new laoy(),new tuon()};
                //立方foreah循环便利来输出老鹰类和鸵鸟类中的接口方法
                foreach (Iflyable outfly in flys)
    
                {
                    outfly.fly(); //用输出outfly参数来输出接口的方法
                }
                Console.ReadKey();
            }
        }
    }
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 接口继承与实施
    {
        interface Iflyable
        {
            //定义一个会飞的一个接口的方法
            void fly();
        }
    }
    
  • 相关阅读:
    nfs目录权限
    14.5.5 AUTO_INCREMENT Handling in InnoDB 在InnoDB AUTO_INCREMENT处理
    Tk 表格的宽度
    化工企业数据分析平台项目之应收款分析
    化工企业数据分析平台项目之应收款分析
    14.5.3 Grouping DML Operations with Transactions 分组DML 事务操作
    perl | 匹配多个
    struts的控制器组件
    解决Thinkpad开启飞行模式无法连接无线网络
    如何解决Thinkpad连接wifi经常断线
  • 原文地址:https://www.cnblogs.com/xiaowie/p/8608109.html
Copyright © 2011-2022 走看看