• 第14节、C#接口Interface


    接口Interface

    简述:接口定义了所有类继承接口时因遵循的语法合同,所以实现接口的类或结构必须实现接口定义中指定的接口成员。

    接口名称

    通常是以I开头,再加上其他单词构成,例如创建一个计算机的接口。

    接口定义成员要求

    • 接口成员不允许使用public、private、protected、internal访问修饰符。
    • 接口成员不允许使用static、virtual、abstract、sealed修饰符。
    • 接口中不允许定义字段
    • 接口中定义的方法不能包含方法体

    声明接口

    interface 接口名称
    {
    接口成员;
    }

    1、单继承接口

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
       public interface I接口
        {
            //获取面积
            decimal GetArea();
            //获取周长
            decimal GetPerimeter(decimal X, decimal Y);
        }
    
       public class 学生A : I接口
       { 
           public decimal longs;
           public decimal Longs { get { return longs; } set { longs = value; } }
           public decimal high;
           public decimal High { get { return high; } set { high = value; } }
    
           public 学生A(decimal longs, decimal high)
           {
               Longs = longs;
               High = high;
           }
           public decimal GetArea()
           {
               return Convert.ToDecimal(longs * high);
           }
           public decimal GetPerimeter(decimal his,decimal lgs)
           {
               return Convert.ToDecimal((his + lgs)*2);
           }
       }
    
       public class 程序入口
       {
           public static void Main(string[] obj)
           {
               学生A A = new 学生A(5,6);
               Console.WriteLine("学生A计算面积:{0}", A.GetArea());
               Console.WriteLine("学生A计算周长:{0}", A.GetPerimeter(2, 3));
               Console.ReadLine();
           }
       }
    }
    View Code

    错误一、结构方法体加访问修饰符执行错误

     2、多继承(接口之间的继承关系)

    C继承B接口,B接口继承A接口,B和A互为继承关系。通过派生类实现接口中所有接口方法。

     

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace ConsoleApplication1
     8 {
     9    public interface I接口
    10     {
    11         //获取面积
    12       decimal GetArea();
    13         //获取周长
    14       decimal GetPerimeter(decimal X, decimal Y);
    15     }
    16 
    17    public interface I接口2 : I接口
    18    {
    19        decimal Getvolume(decimal X, decimal Y, decimal Z);
    20    }
    21 
    22    public class 学生A : I接口2
    23    { 
    24        public decimal longs;
    25        public decimal Longs { get { return longs; } set { longs = value; } }
    26        public decimal high;
    27        public decimal High { get { return high; } set { high = value; } }
    28 
    29        public 学生A(decimal longs, decimal high)
    30        {
    31            Longs = longs;
    32            High = high;
    33        }
    34        public decimal GetArea()
    35        {
    36            return Convert.ToDecimal(longs * high);
    37        }
    38        public decimal GetPerimeter(decimal his,decimal lgs)
    39        {
    40            return Convert.ToDecimal((his + lgs)*2);
    41        }
    42 
    43        public decimal Getvolume(decimal his, decimal lgs, decimal z)
    44        {
    45            return Convert.ToDecimal(his * lgs * z);
    46        }
    47 
    48    }
    49 
    50    public class 程序入口
    51    {
    52        public static void Main(string[] obj)
    53        {
    54            学生A A = new 学生A(5,6);
    55            Console.WriteLine("学生A计算面积:{0}", A.GetArea());
    56            Console.WriteLine("学生A计算周长:{0}", A.GetPerimeter(2, 3));
    57            Console.WriteLine("学生A计算体积:{0}", A.Getvolume(2, 3, 4));
    58            Console.ReadLine();
    59        }
    60    }
    61 }
    View Code

    错误二、未完成实现接口所有方法

     3、接口多重继承 使用逗号分开

  • 相关阅读:
    关于禁止浏览器的回退功能
    实时监听输入框值变化的完美方案:oninput & onpropertychange
    Sublime Text 3 快捷键精华版
    计算手动输入的文本长度
    ie的用户名密码输入框右侧提示去掉
    获取元素定位
    select option
    js打断点
    Android LaunchMode
    Gradle 使用第三方库总结
  • 原文地址:https://www.cnblogs.com/liuzz/p/14521493.html
Copyright © 2020-2023  润新知