C# 如何打印数组中的对象?

C# 如何打印数组中的对象?,c#,arrays,object,C#,Arrays,Object,我正在尝试打印将作为对象插入数组中的多个信息条目,我希望使用这些对象可用的方法并打印结果。这是我的密码。我最后两句话有个错误 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace homework2 { class Shapes { protected string ShapeName; protected

我正在尝试打印将作为对象插入数组中的多个信息条目,我希望使用这些对象可用的方法并打印结果。这是我的密码。我最后两句话有个错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace homework2
{

    class Shapes
    {
        protected string ShapeName;
        protected double ShapeWidth;
        protected double ShapeHeight;

        public Shapes(string ShapeName, double ShapeWidth, double ShapeHeight)

        {
            this.ShapeName = ShapeName;
            this.ShapeWidth = ShapeWidth;
            this.ShapeHeight = ShapeHeight;

        }

    }

    class Rectangle : Shapes 
    {
        public Rectangle(string ShapeName, double ShapeWidth, double ShapeHeight)
            : base(ShapeName, ShapeWidth, ShapeHeight) 
        {
            this.ShapeName = ShapeName;
            this.ShapeWidth = ShapeWidth;
            this.ShapeHeight = ShapeHeight;

        }

        public double GetArea()
        {
            if (ShapeName == "Circle")
            {
                ShapeHeight = 3.14;
                double x = ShapeHeight * (ShapeWidth * ShapeWidth);
                return x;
            }
            else
            {

                double Area = ShapeHeight * ShapeWidth;
                return Area;
            }

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Rectangle Rec = new Rectangle("Circle",5,2);
            System.Console.WriteLine("This is the Rectangle Area :"+Rec.GetArea());

            System.Console.WriteLine("Please Enter How Many Shapes You want To enter:");
            String x =  Console.ReadLine();
            int y = int.Parse(x);
            for (int i = 0; i <= y; i++ )
            {
                System.Console.WriteLine("Enter Name for Shape No."+i+"Please");
                String ShapeName = Console.ReadLine();
                System.Console.WriteLine("Enter width for Shape No." + i + "Please");
                String ShapeWidth = Console.ReadLine();
                int sw = int.Parse(ShapeWidth);
                System.Console.WriteLine("Enter height for Shape No." + i + "Please");
                String ShapeHeight = Console.ReadLine();
                int sh = int.Parse(ShapeHeight);
                for(int j = 0; j < 4; j++)
                {

                      Rectangle[,] z = new Rectangle[y,4];                    
                Rectangle z[i,j] = new Rectangle(ShapeName, sw, sh);
                }


            }
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间作业2
{
类形状
{
保护字符串ShapeName;
保护双形宽度;
受保护的双倍重量;
公共形状(字符串形状名称、双形状宽度、双形状高度)
{
this.ShapeName=ShapeName;
this.ShapeWidth=ShapeWidth;
this.ShapeHeight=ShapeHeight;
}
}
类矩形:形状
{
公共矩形(字符串形状名称、双形状宽度、双形状高度)
:基础(形状名称、形状宽度、形状高度)
{
this.ShapeName=ShapeName;
this.ShapeWidth=ShapeWidth;
this.ShapeHeight=ShapeHeight;
}
公共区域()
{
if(ShapeName==“圆”)
{
形高=3.14;
双x=形高*(形宽*形宽);
返回x;
}
其他的
{
双面积=形高*形宽;
返回区;
}
}
}
班级计划
{
静态void Main(字符串[]参数)
{
矩形Rec=新矩形(“圆”,5,2);
System.Console.WriteLine(“这是矩形区域:”+Rec.GetArea());
System.Console.WriteLine(“请输入要输入的形状数:”);
字符串x=Console.ReadLine();
int y=int.Parse(x);

对于(int i=0;i,首先,在派生类矩形中,不需要重新分配shape中的变量,因为基本构造函数仍将被调用

另外,创建一个新的类Circle(实现了不同GetArea()的形状)比让矩形类计算圆的面积更有意义,而不是传入类似“Circle”的字符串来创建圆

您可能遇到的错误与以下行有关:

Rectangle z[i,j] = new Rectangle(ShapeName, sw, sh);
因为您已经将z[i,j]定义为数组,所以这一行应该是

z[i,j] = new Rectangle(ShapeName, sw, sh);
(不带矩形)

但是,我怀疑您想在第一个for循环之外定义矩形数组。使用当前代码,最终将得到y 2D数组,每个数组填充一列。您需要将其移动到第一个for循环之外
矩形[,]z=new Rectangle[y,4];

出现了什么错误?请随意删除无关代码……第一个错误出现在最后一个z上“局部变量名“z”已经在范围中定义了”,在数组中表示“数组减速差,等于它所说的值”;ecpected“和new”:expected“你所说的外来代码是什么意思?请解释一下