C#学习笔记之——接口实例

using System;

namespace test_2_1
{
    interface IDimensions  //声明接口IDimensions
    {
        float getLength();  //包含的方法1
        float getWidth();   //包含的方法2
    }

    class Box : IDimensions   //声明类Box继承于接口IDimensions
    {
        float lengthInches;
        float widthInches;

        Box(float length, float width)    
        {
            lengthInches = length;
            widthInches = width;
        }
        // Explicit interface member implementation: 
        float IDimensions.getLength()    //该类显式实现接口成员 getLength 和 getWidth
        {
            return lengthInches;
        }
        // Explicit interface member implementation:
        float IDimensions.getWidth()
        {
            return widthInches;
        }

        static void Main()
        {
            // Declare a class instance box1:
            Box box1 = new Box(30.0f, 20.0f);

            // Declare an interface instance dimensions:
            IDimensions dimensions = (IDimensions)box1; 

            // The following commented lines would produce compilation 
            // errors because they try to access an explicitly implemented
            // interface member from a class instance:                   
            //System.Console.WriteLine("Length: {0}", box1.getlength());  //错误的原因是不能直接实例化接口
            //System.Console.WriteLine("Width: {0}", box1.getwidth());

            // Print out the dimensions of the box by calling the methods 
            // from an instance of the interface:
            System.Console.WriteLine("Length: {0}", dimensions.getLength());
            System.Console.WriteLine("Width: {0}", dimensions.getWidth());
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

显式 接口实现还允许程序员实现具有相同成员名称的两个接口,并为每个接口成员各提供一个实现。 本示例同时以公制单位和英制单位显示框的尺寸。Box 类实现 IEnglishDimensions 和 IMetricDimensions 两个接口,它们表示不同的度量系统。 两个接口有相同的成员名称 Length 和 Width。

using System;

namespace test_2_2
{
    // Declare the English units interface:
    interface IEnglishDimensions
    {
        float Length();
        float Width();
    }

    // Declare the metric units interface:
    interface IMetricDimensions
    {
        float Length();
        float Width();
    }

    // Declare the Box class that implements the two interfaces:
    // IEnglishDimensions and IMetricDimensions:
    class Box : IEnglishDimensions, IMetricDimensions
    {
        float lengthInches;
        float widthInches;

        public Box(float length, float width)
        {
            lengthInches = length;
            widthInches = width;
        }

        // Explicitly implement the members of IEnglishDimensions:
        float IEnglishDimensions.Length()
        {
            return lengthInches;
        }

        float IEnglishDimensions.Width()
        {
            return widthInches;
        }

        // Explicitly implement the members of IMetricDimensions:
        float IMetricDimensions.Length()
        {
            return lengthInches * 2.54f;
        }

        float IMetricDimensions.Width()
        {
            return widthInches * 2.54f;
        }

        static void Main()
        {
            // Declare a class instance box1:
            Box box1 = new Box(30.0f, 20.0f);

            // Declare an instance of the English units interface:
            IEnglishDimensions eDimensions = (IEnglishDimensions)box1;

            // Declare an instance of the metric units interface:
            IMetricDimensions mDimensions = (IMetricDimensions)box1;

            // Print dimensions in English units:
            System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
            System.Console.WriteLine("Width (in): {0}", eDimensions.Width());

            // Print dimensions in metric units:
            System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
            System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值