zoukankan      html  css  js  c++  java
  • Interface定义及使用

         接口定义以大写字母I开头。方法只定义其名称,在C#中,方法默认是公有方法;用public修饰方法是不允许的,否则会出现编译错误;接口可以从别的接口继承,如果是继承多个接口,则父接口列表用逗号间隔。
          接口可以通过类来实现,当类的基列表同时包含基类和接口时,列表中首先出现的是基类;类必须要实现其抽象方法; 
          接口使用:见代码(转)
          

    interface使用

    interface使用(实例一)
     
    using System;
    namespace Dage.Interface
    {
     //打印机接口
     public interface IPrint
     {
      string returnPrintName();
     }
    }
    //--------------------------------------------
    using System;
    using Dage.Interface;
    namespace Dage.Print
    {
     //HP牌打印机类
     public class HP: IPrint
     {
      public string returnPrintName()
      {
       return "这是HP牌打印机";
      }
     }
    }
    //--------------------------------------------
    using System;
    namespace Dage.Print
    {
     //Eps牌打印机类
     public class Eps: IPrint
     {
      public string returnPrintName()
      {
       return "这是Eps牌打印机";
      }
     }
    }
    //--------------------------------------------
    using System;
    using Dage.Interface;
    namespace Dage
    {
     //打印类
     public class Printer
     {
      public Printer()
      {}
      public string PrintName(IPrint iPrint)
      {
       return iPrint.returnPrintName();
      }
     }
    }
    //--------------------------------------------
    --WinFrom中调用代码:
    private void button1_Click(object sender, System.EventArgs e)
    {
     Printer p= new Printer();
     switch (this.comboBox1.Text)
     {
      case "HP":
       MessageBox.Show(p.PrintName(new HP()));
       break;
      case "Eps":
       MessageBox.Show(p.PrintName(new Eps()));
       break;
      default:
       MessageBox.Show("没有发现这个品牌!");
       break;
     }
    }
  • 相关阅读:
    python安装cnstd卡住
    _、__、__xx__之间的差别
    Celery模块使用
    同一主机,开启多个不同端口的redis进程
    php配置变更记录
    Linux安装Nodejs
    ElasticSearch中term和match探索
    Centos安装elasticsearch,php连接使用
    centos8自定义目录安装php7.3
    centos8自定义目录安装nginx
  • 原文地址:https://www.cnblogs.com/kedach/p/658364.html
Copyright © 2011-2022 走看看