zoukankan      html  css  js  c++  java
  • 接口

    接口,是一个约定,接口中有多个抽象方法,接口能实现多继承

    实现了不相关类的,相同行为!

    public interface IStringList

    {

      void Add(string s);//public abstract 不写出来,方法

      int Count {get;}//public abstract 不写出来,属性

      string this[int index] {get; set;}  //public abstract 不写出来,索引

    }

    如果不同接口,有同名的方法,为防止歧义,在方法名前加接口名,调用时只能用接口调用

    ((IWindows) f  )  .Close();

     1 using System;
     2 
     3 interface Runner 
     4 { 
     5     void run();
     6 }
     7 
     8 interface Swimmer 
     9 { 
    10     void swim()
    11 }
    12 
    13 abstract class Animal  
    14 {   
    15     abstract public void eat();
    16 }  
    17 
    18 class Person : Animal , Runner, Swimmer 
    19 {
    20     public void run() 
    21     { 
    22         Console.WriteLine("run"); 
    23     }
    24 
    25     public void swim()  
    26     {
    27         Console.WriteLine("swim"); 
    28     }
    29 
    30     public override void eat() 
    31     { 
    32         Console.WriteLine("eat"); 
    33     }
    34     
    35     public void speak()
    36     {
    37         Console.WriteLine("speak"); 
    38     }
    39 }
    40 
    41 class TestInterface
    42 {
    43     static void m1(Runner r) 
    44     { 
    45         r.run(); 
    46     }
    47 
    48     static void m2(Swimmer s) 
    49     { 
    50         s.swim(); 
    51     }
    52 
    53     static void m3(Animal a) 
    54     {
    55         a.eat(); 
    56     }
    57 
    58     static void m4(Person p)
    59     {
    60         p.speak();
    61     }
    62 
    63     public static void Main(string [] args)
    64     {
    65         Person p = new Person();
    66         m1(p);
    67         m2(p);
    68         m3(p);
    69         m4(p);
    70 
    71         Runner a = new Person();
    72         a.run();
    73     }
    74 }
    继承、接口实现
     1 using System;
     2 
     3 class InterfaceExplicitImpl
     4 {
     5     static void Main()
     6     {   //1.
     7         FileViewer f = new FileViewer();
     8         f.Test();// 接口强转
     9         
    10         ( (IFileHandler) f ).Close();
    11         
    12         //2.
    13         IWindow w = new FileViewer();//接口引用
    14         w.Close();
    15         
    16         IFileHandler fh = new FileViewer();
    17         fh.Close();
    18     }
    19 }
    20 
    21 interface IWindow
    22 {
    23     void Close();
    24 }
    25 
    26 interface IFileHandler
    27 {
    28     void Close();
    29 }
    30 
    31 class FileViewer : IWindow, IFileHandler
    32 {
    33     void IWindow.Close ()
    34     {
    35         Console.WriteLine( "Window Closed" );
    36     }
    37 
    38     void IFileHandler.Close()
    39     {
    40         Console.WriteLine( "File Closed" );
    41     }
    42 
    43     public void Test()
    44     {
    45         ( (IWindow) this ).Close();
    46     }
    47 }
    多接口,同名方法处理
  • 相关阅读:
    利用SVN进行个人代码管理
    ECEF坐标系
    地理坐标系、大地坐标系、投影坐标系
    让VS能够识别我的DLL运行库
    cannot convert parameter 1 from 'const char *' to 'LPCWSTR' 修改
    创建文件目录C++ windows
    GDAL获取遥感图像基本信息
    全球经纬度划分
    遥感影像度与米的转换
    C++ assert用法
  • 原文地址:https://www.cnblogs.com/GoldenEllipsis/p/10462949.html
Copyright © 2011-2022 走看看