类方法未通过

问题描述 投票:0回答:1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CircleArea
{

    internal class Program
    {
        //create a base class BasicShape 
        abstract class BasicShape
        {

            public int dimensionOne;
            public abstract double area();


        }

        class Circle : BasicShape 
        {
            override public double area() 
            { 
                return (dimensionOne * dimensionOne * 3.14);
            }
        }
        static void Main(string[] args)
        {

            BasicShape[] circle = new BasicShape[10];
            for (int i = 0; i < circle.Length; i++) 
            {
                circle[i] = new Circle();
                Console.WriteLine($"The area of a circle with a radius of {circle[i+1]} is {circle.area()}");



            }
        }
    }
}

为什么这不起作用? area() 在基类中创建,然后传递给派生类,派生类默认覆盖区域,然后使用分配了圆类方法的基类实例化一个数组对象。我很大程度上是根据我的教授今天在课堂上所做的事情得出的,我精神疲惫,而他在分配任务的当天就完成了任务。

c# abstract derived-class class-method
1个回答
0
投票

我在 Main 函数中进行了此更改,并且代码运行良好:

        static void Main(string[] args)
    {

        BasicShape[] circle = new BasicShape[10];
        Random rnd = new Random(); // made a random obj, for fill the Array with some stuff
        for (int i = 0; i < circle.Length; i++)
        {
            circle[i] = new Circle(); // // Creating a new instance of the Circle
            circle[i].dimensionOne = rnd.Next(1, 10); //  filling the array
            Console.WriteLine($"The area of a circle with a radius of {circle[i].dimensionOne} is {circle[i].area()}"); // on this part u was treating the array as a variable so i just put [i] and now u can access to the area() method



        }
    }

抱歉,如果我没有那么解释,但我很快回答你,因为我有点忙。但我希望这对你有用。和平伙伴。

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