尝试理解接口

问题描述 投票:0回答:1

我是 C# 新手,很难理解接口的使用和好处。 我得到了下面的例子(例1)来研究接口的使用。 我的问题是该程序在没有界面的情况下也能正常工作(示例 2)。 那么这个例子中的接口有什么好处呢?

示例1:

using System;

namespace ConsoleApp1
{
    interface IShape
    {
        double Area();
        double Perimeter();
    }

    class Circle : IShape
    {
        private double radius;
        public Circle(double radius)
        {
            this.radius = radius;
        }

        public double Area()
        {
            return Math.PI * radius * radius;
        }

        public double Perimeter()
        {
            return 2 * Math.PI * radius;
        }
    }

    class Rectangle : IShape
    {
        private double length;
        private double width;

        public Rectangle(double length, double width)
        {
            this.length = length;
            this.width = width;
        }

        public double Area()
        {
            return length * width;
        }

        public double Perimeter()
        {
            return 2 * ( length + width );
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            IShape circle = new Circle(5);
            IShape rectangle = new Rectangle(10, 20);;

            Console.WriteLine("Cirkelns area är {0}", circle.Area());
            Console.WriteLine("Cirkelns omkrets är {0}", circle.Perimeter());
            Console.WriteLine("Rektangelns area är {0}", rectangle.Area());
            Console.WriteLine("Rektangelns omkrets är {0}", rectangle.Perimeter());
        }
    }
}

示例2:

using System;

namespace ConsoleApp1
{

    class Circle
    {
        private double radius;
        public Circle(double radius)
        {
            this.radius = radius;
        }

        public double Area()
        {
            return Math.PI * radius * radius;
        }

        public double Perimeter()
        {
            return 2 * Math.PI * radius;
        }
    }

    class Rectangle
    {
        private double length;
        private double width;

        public Rectangle(double length, double width)
        {
            this.length = length;
            this.width = width;
        }

        public double Area()
        {
            return length * width;
        }

        public double Perimeter()
        {
            return 2 * ( length + width );
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Circle circle = new Circle(5);
            Rectangle rectangle = new Rectangle(10, 20);

            Console.WriteLine("Cirkelns area är {0}", circle.Area());
            Console.WriteLine("Cirkelns omkrets är {0}", circle.Perimeter());
            Console.WriteLine("Rektangelns area är {0}", rectangle.Area());
            Console.WriteLine("Rektangelns omkrets är {0}", rectangle.Perimeter());
        }
    }
}

我研究并计算了这两个示例,试图理解该接口的作用以及为什么我要使用它

c# interface
1个回答
0
投票

如果您对 n 个形状的面积总和感兴趣,您不能只迭代混合内容的列表,例如

List<Circle Or Rectangle>
...所以您只需使用
List<IShape>
。然后,如果它实现了接口函数,您就可以调用该列表中任何内容的“area()”函数。

© www.soinside.com 2019 - 2024. All rights reserved.