c#实现接口类的继承

(1) 编写两个接口,接口 IShape 包含三个方法:initialize, getPerimeter, getArea。分

别进行初始化、获取边长和面积,其返回值均为 decimal。接口 IDisplayresult 显示计算结果。 

(2) 编写两个类,Square(正方形)和 Circle(圆形),实现 IShape 和 IDisplayresult

接口。 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace C_Lab2_2

{

    class Program

    {

        interface shape

        {

            public void initialize(); //初始化

            public decimal getPerimeter();// 获取边长

            public decimal getArea(); //获取面积

 

        }

        interface Displayresult

        {

            public void Display();

 

        }

        class square : shape,Displayresult{

        private decimal length;

            public void initialize() {

                Console.WriteLine("请输入正方形边长");

                length = decimal.Parse(Console.ReadLine());

            }

            public decimal getPerimeter() {

 

 

                return length;

            }// 获取边长

            public decimal getArea() {

                return length*length;

            } //获取面积

            public void Display() {

                Console.WriteLine("正方形面积为:");

                Console.WriteLine(getArea());

 

            }

        }

        class circle : shape, Displayresult

        {

            private decimal adius;

            public void initialize()

            {

                Console.WriteLine("请输入圆形半径");

                adius = decimal.Parse(Console.ReadLine());

            }

            public decimal getPerimeter()

            {

 

 

                return adius;

            }// 获取边长

            public decimal getArea()

            {

                return 3*adius * adius;

            } //获取面积

            public void Display()

            {

                Console.WriteLine("圆形面积为:");

                Console.WriteLine(getArea());

 

            }

        }

        static void Main(string[] args)

        {

            square square = new square();

            square.initialize();

            square.Display();

            circle circle = new circle();

            circle.initialize();

            circle.Display();

        }

    }

}

posted on 2021-11-14 12:20  风中明月  阅读(592)  评论(0编辑  收藏  举报