zoukankan      html  css  js  c++  java
  • c#学习笔记02——接口

    本身并不实现功能,但提供一种模板定义,为从它继承类或结构提供了一种定义的规范

    有了接口,程序员可以把程序定义的更积极啊清晰和条理化

    • 理解接口
      1. 接口支持多继承;抽象类不能实现多继承
      2. 接口只能定义抽象规则;抽象类即可以定义规则,还可以提供已实现的成员
      3. 接口是一组行为规范;抽象类是一个不完全的类,着重族的概念
      4. 接口可以用于支持回调;抽象类不能支持回调,因为继承不支持
      5. 接口只包含方法、属性、索引器、事件的签名,但不能定义字段和包含实现的方法;抽象类可以定义字段、属性、包含有实现的方法
      6. 接口刻意作用于值类型和引用类型;抽象类只能作用于引用类型
      • 作用上的区别 抽象类&接口
        1. 抽象类 是对象的抽象,着重表现在继承上,从抽象类派生的类和抽象类存在派生关系
        2. 接口 用来定义一组行为规范,一旦一个类从一个接口继承后,使用这个类的程序员就会知道这个类肯定包含接口定义的行为,而不用关心如何实现
           1 using cs002;
           2 using System;
           3 using System.Collections.Generic;
           4 using System.Linq;
           5 using System.Text;
           6 using System.Threading.Tasks;
           7 
           8 
           9 namespace cs002
          10 {
          11     //定义抽象类
          12     public abstract class Travel
          13         {
          14             protected string _name;
          15             public abstract string Name//抽象属性
          16             {
          17                 get;
          18                 set;
          19             }
          20             public void Show()
          21             {
          22                 Console.WriteLine("这是{0}", _name);
          23             
          24             }
          25             public abstract void GetWheel();
          26         }
          27         //定义接口
          28         interface IAction
          29         {
          30             void Move();
          31         }
          32        interface test
          33     {
          34         void fuck();
          35     }
          36     
          37        public class Car:Travel,IAction,test
          38         {
          39             public override string Name
          40             {
          41                 get
          42                 {
          43                     return _name;
          44                 }
          45 
          46                 set
          47                 {
          48                     _name = value;
          49                 }
          50             }
          51             public Car(string name)
          52             { _name = name; }
          53         
          54             public override void GetWheel()
          55             {
          56                 Console.WriteLine("小汽车有四个轮子");
          57             }
          58             public void Move()
          59             {
          60                 Console.WriteLine("小汽车行走在公路上");
          61             }
          62         public void fuck()
          63         {
          64             Console.WriteLine("fuck it!");
          65         }
          66     }
          67         }
          68     class Program
          69     {
          70         static void fun()
          71         {
          72             for (int i = 0; i < 10; i++)
          73                 Console.Write(i + " ");
          74             Console.WriteLine(" ");
          75         }
          76         static void Main(string[] args)
          77         {
          78         Car c0 = new Car("宇宙无敌");
          79         c0.GetWheel();
          80         c0.Move();
          81         c0.fuck();
          82         }
          83     }
          View Code
    • 实现接口 接口是在其派生类中完成的 在c#中,派生类必须实现接口定义的成员;一个类可以派生自多个接口,接口之间用逗号隔开
      1. 实现接口↑
      2. 显式实现:继承多个接口,接口之间有重名的方法或属性;当显示实现接口时,类成员只能通过接口使用
         1 using cs002;
         2 using System;
         3 using System.Collections.Generic;
         4 using System.Linq;
         5 using System.Text;
         6 using System.Threading.Tasks;
         7 
         8 
         9 namespace cs002
        10 {
        11     interface It1
        12     {
        13         void fuck();
        14     }
        15     interface It2
        16     {
        17         void fuck();
        18     }
        19     public class test : It1, It2
        20     {
        21         public test() { }
        22         void It1.fuck()
        23         {
        24             Console.WriteLine("fuck you!");
        25         }
        26         void It2.fuck()
        27         {
        28             Console.WriteLine("fuck me!");
        29         }
        30     }
        31     class Program
        32     {
        33         static void fun()
        34         {
        35             for (int i = 0; i < 10; i++)
        36                 Console.Write(i + " ");
        37             Console.WriteLine(" ");
        38         }
        39         static void Main(string[] args)
        40         {
        41             test t = new test();
        42             It1 t1 = (It1)t;
        43             t1.fuck();
        44             It2 t2 = (It2)t;
        45             t2.fuck();
        46 
        47         }
        48     }
        49 }
        View Code
  • 相关阅读:
    手把手实战:eclipse 搭建 SpringMvc 框架环境
    解决eclipse中Tomcat服务器的server location选项不能修改的问题
    如何解决JSP页面顶端报错 The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
    第二课 --- git的(管理修改和撤销修改、删除文件)
    第二课 ---git时光穿梭(版本回退)
    第一课——git的简介和基本使用
    001——使用composer安装ThinkPHP5
    微信小程序中对于变量的定义
    微信小程序onLaunch修改globalData的值
    7——ThinkPhp中的响应和重定向:
  • 原文地址:https://www.cnblogs.com/yuelien/p/6623583.html
Copyright © 2011-2022 走看看