zoukankan      html  css  js  c++  java
  • 显示接口和隐式接口的区别:

    一、新建一个接口类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// InterF 的摘要说明
    /// </summary>
    public class InterF
    {
        public interface hide
        {
            string show();
        }
        public interface display
        {
            string show();
        }
    
    }
    

    二、新建接口实现类,hide.cs和display.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Speaker 的摘要说明
    /// </summary>
    
    public class hide : InterF.hide
    {
        public string show()
        {
            return "隐示接口";
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    /// <summary>
    /// Speaker1 的摘要说明
    /// </summary>
    public class display : InterF.display
    {
        string InterF.display.show()
        {
            return "显示接口";
        }
    }
    

    三、新建一个default.aspx文件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    public partial class Default3 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InterF.hide c = new hide();
            c.show();
            hide s = new hide();
            s.show();
            Label1.Text = s.show() + c.show();
    
    
            InterF.display c1 = new display();
            c1.show();
            Label2.Text = c1.show();
            //下面写法将引起编译错误错误“PetShop.Speaker”不包含“Speak”的定义
            //display s1 = new display();
            //s1.Speak();
        }
    }
    

    四、综上总结:显示接口和隐式接口的区别

     <1>隐示实现接口和类都可以访问 显示实现只有接口可以访问。

    <2>显示实现益处

    1:隐藏代码的实现功能

    2:在使用接口访问的系统中,调用者只能通过接口调用而不是底层的类来访问,有利于安全性

     综上:当类或结构继承接口时,意味着该类或结构为该接口定义的所有成员提供实现。接口本身不提供类或结构能够以继承基类功能的方式继承的任何功能。基类实现接口,派生类将继承该实现

  • 相关阅读:
    2017-2018-2 20155206 《网络对抗技术》 实验六:信息搜集与漏洞扫描
    20155206 Exp5 MSF基础应用
    20155206 实验4 恶意代码分析
    20155206《网络攻防》第三次实验_免杀及其原理
    20155206 Exp2 后门原理与实践
    20155206赵飞 Exp1PC平台逆向破解及Bof基础实践
    J-19 集合对象
    J-18 集合对象
    J-17 集合对象
    J-16 集合对象
  • 原文地址:https://www.cnblogs.com/May-day/p/5389091.html
Copyright © 2011-2022 走看看