0

我需要进一步了解IInterface与抽象类一起使用的类。是否曾经使用过这些东西都有工作要求,需要更多地了解它。

我认为一个接口是(这工作并使用提供的值计算 3 个项目的面积Rectangle):TriangleCircle

static void Main(string[] args)
{
    Rectangle r = new Rectangle(4, 8, "Rectangle");

    Triangle t = new Triangle(4, 4, "Triangle");

    Circle c = new Circle(3, "Circle");

    r.DisplayArea();
    Console.WriteLine();
    t.DisplayArea();
    Console.WriteLine();
    c.DisplayArea();
    Console.ReadKey();
}

这是我的抽象类。看起来它处理的是被测量的名称:

namespace DrawShapes
{
    abstract class Shape
    {
        public string name;

        public abstract void DisplayArea();

        public Shape(string name)
        {
            this.name = name;
        }
    }

我不确定,但这些不会在IInterface课堂上并以某种方式设置或调用吗?绝对不是这种面向对象的专家。

public int Radius { get; set; }
public int Height { get; set; }
public int Width { get; set; }

相反,我将参数硬编码到“函数调用”中:

static void Main(string[] args)
{
    Rectangle r = new Rectangle(4, 8, "Rectangle");

    Triangle t = new Triangle(4, 4, "Triangle");

    Circle c = new Circle(3, "Circle");
}

我个人想不出为什么或如何使用IInterface.

只是为了完整,也许可以帮助正在寻找这样的东西的人,这是我计算面积的类。计算面积还有其他 SO 答案,这个问题是关于IInterface抽象类的:

namespace DrawShapes
{
    class Rectangle : Shape
    {
        public int length;
        public int width;

        public Rectangle(int length, int width, string name) : base(name)
        {
            //this.angles = angles;
            this.length = length;
            this.width = width;
        }

        public override void DisplayArea()
        {
            Console.Write(name + "--");
            Console.Write(length * width);
        }
    }

    class Triangle : Shape
    {
        // public int angles;
        public int width;
        public int height;

        public Triangle(int width, int height, string name) : base(name)
        {
            //this.angles = angles;
            this.width = width;
            this.height = height;
        }

        public override void DisplayArea()
        {
            Console.Write(name + "--");
            Console.Write((width * height) / 2);
        }
    }

    class Circle : Shape
    {
        public int radius;

        public Circle(int radius, string name) : base(name)
        {
            this.radius = radius;
        }

        public override void DisplayArea()
        {
            Console.Write(name + "--");
            Console.Write(3.22 * radius * radius);
        }
    }
}
4

0 回答 0