面向对象程序设计中接口(interface)的使用

240 阅读5分钟

一起养成写作习惯!这是我参与「掘金日新计划 · 4 月更文挑战」的第6天,点击查看活动详情

最近工作中需要使用到接口interface。可是接口是用来做什么的?我看了一些网上的例子,目前我看到的大多数接口的功能为一下两点:

1 :接口作为约束与规范

我们可以根据需求来定义接口,然后我们再定义类来实现这个接口。接口为一个或多个类提供规范。

2 :优化程序设计

面向对象设计中我们追求的原则之一就是高内聚,低耦合。可是类与类之间往往会有千丝万缕的关系,比如泛化、实现、组合、聚合、关联、依赖。而接口则可以将一个类对另一个类的依赖性降到最低,这就是【接口隔离】

 

举个例子:

我们现在有三个类:行为类、猫类、金鱼类。

行为类中定义了各种动物的行为,比如:抓老鼠,吐泡泡,挖洞,游泳等。

猫类需要使用到行为类中的抓老鼠方法。

金鱼类需要使用到行为类中的吐泡泡方法。

如果我们不使用接口,我们将把行为类中的所有方法都授予猫类和金鱼类,这显然是不合适的。

如果使用接口,我们定义两个接口,一个猫接口,里边一个抓老鼠方法。一个金鱼接口,里边一个吐泡泡方法。这两个接口,都由行为类来实现。这样猫类和金鱼类就使用到了他们各自所需要的方法,而其他行为方法并没有授予给他们。

实现这个例子之前呢,我们来看下接口的基础知识。

在定义接口时候要注意如下几点:

(1)接口声明不能包含以下成员:

数据成员、静态成员。

(2)接口声明只能包含如下类型的非静态成员函数的声明:

方法、属性、事件、索引器。

(3)这些函数成员的声明不能包含任何实现代码,而在每一个成员声明的主体后必须使用分号结尾。

(4)定义接口命名基本上以大写的 ‘I’ 开头,当然这个不是一定的。只是一种个人习惯。

(5)接口声明可以有任何的访问修饰符,public、protected、internal、private

(6)接口中的方法默认public ,且不允许添加任何修饰符

(7)实现类必须实现其所要实现的接口类中的所有方法

(8)接口是可以多继承继承的,但是可以多层继承。

(9)接口不能实例化(创建对象),只能被实现。

 

实例代码:声明接口(这里以C#作为例子)

Public interface InterfaceGc
{
 // 存款
 void PayIn(decimal amount);
 // 取款
 bool QuKuan(decimal amount);
 // 余额(只读)
 decimal Sheng{
    get;
 }
}

 

接口单继承:

/// <summary>
        /// 定义一个存取款接口
        /// </summary>
        interface InterfaceGc
        {
            // 存款
            void PayIn(decimal amount);
            // 取款
            bool QuKuan(decimal amount);
            // 余额(只读)
            decimal Sheng
            {
                get;
            }
        }
 
        /// <summary>
        /// 定义一个转账接口继承存取款接口,只能是单继承
        /// </summary>
        interface InterfaceNew : InterfaceGc
        {
            // 定义一个接口方法
            bool TranceTo(InterfaceGc destination, decimal amount);
        }

 

下边我写了两个例子,一个是刚刚上边说到的两个动物的行为,另一个是关于银行取钱存钱及转账的例子

1:动物行为:我这里使用的是控制台应用程序。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace @interface
{
    class Program
    {
        static void Main(string[] args)
        {
 
            cat catss = new cat();
            catss.iscat();
 
            Console.WriteLine("-------------------------------------------");
 
            fish fishsss = new fish();
            fishsss.isfish();
 
            Console.ReadLine();
        }
    }
 
    /// <summary>
    /// 行为类,同时实现Icat和Ifish两个接口
    /// </summary>
    public class xingwei : Icat,Ifish
    {
        public void zhua()
        {
            Console.WriteLine("抓老鼠");
        }
 
        public void pao()
        {
            Console.WriteLine("吐泡泡");
        }
 
        public void swim()
        {
            Console.WriteLine("游泳");
        }
    }
 
    /// <summary>
    /// 猫类
    /// </summary>
    public class cat
    {
        public Icat cats = new xingwei();
        public void iscat()
        {
            Console.WriteLine("我是一只猫");
            cats.zhua();
        }
    }
 
    /// <summary>
    /// 金鱼类
    /// </summary>
    public class fish
    {
        // 接口不能实例化,但是我们可以实例化实现接口的类
        // 通过这个类来调用你所需要的方法
        public Ifish fishes = new xingwei();
        public void isfish()
        {
            Console.WriteLine("我是一只金鱼");
            fishes.pao();
        }
    }
 
    /// <summary>
    /// 猫专用接口
    /// </summary>
    public interface Icat
    {
        void zhua();
    }
 
    /// <summary>
    /// 鱼专用接口
    /// </summary>
    public interface Ifish
    {
        void pao();
    }
}

 

2:银行存取款及转账方法:控制台应用程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace interfaceBank
{
    class Program
    {
        static void Main(string[] args)
        {
            // 调用继承接口的类(注意这个写法---多态)
            // 接口的对象可以调用类中的普通方法
            InterfaceGc myAccount = new Bank();
            InterfaceNew youAccount = new Inter();
            //我先存钱
            myAccount.PayIn(1000);
            //我再取钱
            myAccount.QuKuan(1200);
            // 你先存钱
            youAccount.PayIn(5000);
            // 你给我转账
            youAccount.TranceTo(myAccount, 500);
            // 查看我的余额
            decimal myYue;
            myYue = myAccount.Sheng;
            Console.WriteLine("我的余额:" + myYue);
            //查看你的余额
            decimal youYue;
            youYue = youAccount.Sheng;
            Console.WriteLine("你的余额:" + youYue);
 
            Console.ReadLine();
        }
 
        /// <summary>
        /// 银行类:实现存取款接口
        /// </summary>
        class Bank : InterfaceGc
        {
            // 定义一个余额
            private decimal blanace;
            // 重写接口中定义好的存钱类
            public void PayIn(decimal amount)
            {
                blanace += amount;
            }
            // 重写接口中定义好的取钱类
            public bool QuKuan(decimal amount)
            {
                // 判断余额是否大于要取得钱数
                if (blanace > amount)
                {
                    blanace -= amount;
                    return true;
                }
                else
                {
                    Console.WriteLine("余额不足,取款失败");
                    return false;
                }
            }
            // 重写余额接口
            public decimal Sheng
            {
                get
                {
                    return blanace;
                }
            }
        }
 
        /// <summary>
        /// 定义一个转账类实现转账接口
        /// </summary>
        class Inter : InterfaceNew
        {
            // 定义一个余额
            private decimal blanace;
            // 重写接口中定义好的存钱类
            public void PayIn(decimal amount)
            {
                blanace += amount;
            }
            // 重写接口中定义好的取钱类
            public bool QuKuan(decimal amount)
            {
                // 判断余额是否大于要取得钱数
                if (blanace > amount)
                {
                    blanace -= amount;
                    return true;
                }
                else
                {
                    Console.WriteLine("余额不足,取款失败");
                    return false;
                }
            }
            // 重写余额接口
            public decimal Sheng
            {
                get
                {
                    return blanace;
                }
            }
            // 转账接口
            public bool TranceTo(InterfaceGc obj, decimal amount)
            {
                bool result = QuKuan(amount);
                if (result == true)
                {
                    obj.PayIn(amount);
                }
                return result;
            }
        }
 
        /// <summary>
        /// 定义一个存取款接口
        /// </summary>
        interface InterfaceGc
        {
            // 存款
            void PayIn(decimal amount);
            // 取款
            bool QuKuan(decimal amount);
            // 余额(只读)
            decimal Sheng
            {
                get;
            }
        }
 
        /// <summary>
        /// 定义一个转账接口继承存取款接口,只能是单继承
        /// </summary>
        interface InterfaceNew : InterfaceGc
        {
            // 定义一个接口方法
            bool TranceTo(InterfaceGc destination, decimal amount);
        }
    }
}

有好的建议,请在下方输入你的评论。

欢迎访问个人博客 guanchao.site