【问题标题】:Basic c# array printing基本 c# 数组打印
【发布时间】:2016-11-18 06:40:39
【问题描述】:

我设法让程序接受值,但我不太确定如何打印我的结果,当我尝试像这样打印它时,这是错误的,它只是给了我随机文本。我想应该是 sh.display 之类的,但我真的迷路了

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

namespace ConsoleApplication1
{
    abstract class shape
    {
        void getarea()
        { Console.WriteLine("Area is"); }
        void display()
        {
            Console.WriteLine("This is shape");
        }   
        void getcirc()
        {
            Console.WriteLine("Circumference is");
        }
    }

    class circle : shape
    {
       public circle(double r)
       {this.r = r;}
        double r;
        void getarea()
        {
            Console.WriteLine("Circle area =" + (Math.PI * Math.Pow(r, 2)));
        }

        void getcirc()
        {
            Console.WriteLine("Circle circumference = " + (2 * Math.PI * r));
        }

        void display()
        {
            Console.WriteLine("This is circle");
        }
    }

    class rect : shape
    {
        public rect(double x, double y)
        {this.x=x;
            this.y=y;
        }
        double x,y;
        void getarea()
        {
            Console.WriteLine("Rectangle area =" + (x * y));
        }

        void getcirc()
        {
            Console.WriteLine("Circle circumference = " + ((2 * x) + (2 * y)));
        }

        void display()
        {
            Console.WriteLine("This is Rectangle");
        }

        class square : shape
        {
            public square(double z)
            {this.z=z;
            }
            double z;
            void getarea()
            {
                Console.WriteLine("Square area =" + (z*z));
            }

            void getcirc()
            {
                Console.WriteLine("Square perimeter = " + (4 * z));
            }

            void display()
            {
                Console.WriteLine("This is square");
            }




            class Program
            {
                static void Main(string[] args)
                {
                    shape[] sh = new shape[15];
                    Random rndm= new Random();

                    int i;
                    int shapenum;
                    for (i = 0; i < 15; i++)
                    { shapenum = rndm.Next(1,4);

                    switch (shapenum)
                    {
                        case 1:
                            Console.WriteLine("Enter radius");
                            sh[i] = new circle(int.Parse(Console.ReadLine()));
                            break;

                        case 2:

                            Console.WriteLine("Enter x and y lengths");
                            double x = double.Parse(Console.ReadLine());
                            double y = double.Parse(Console.ReadLine());
                            sh[i] = new rect(x,y);
                            break;

                        case 3:

                            Console.WriteLine("Enter side length");
                            sh[i] = new square(int.Parse(Console.ReadLine()));
                            break;
                    }
                    }
                    for (i = 0; i < 15; i++)
                    {
                        Console.Write(sh[i] + " ");
                    }


                }
            }
        }
    }
}

【问题讨论】:

  • 定义“给我随机文本”...
  • ConsoleApplication1.rect ConsoleAppication1.rect CosnoleApplication1.circle ConsoleApplication1.Circle ConsoleApplication1.rect+square
  • 这不是随机的。这正是它应该打印的内容。尝试将Console.Write(sh[i] + " "); 替换为sh[i].display();
  • 请澄清您的具体问题或添加其他详细信息以准确突出您的需要。正如目前所写的那样,很难准确地说出你在问什么。请参阅“如何提问”页面以获得澄清此问题的帮助。
  • 哦,我应该输入 15 个随机元素(三种形状之一),然后使用循环打印每个元素的数据(面积、周长/周长和类型),使用三种方法(显示, getarea 和 getcirc) 在每个类中。它还说 shape.display() 由于其保护级别而无法访问

标签: c# arrays object printing abstract


【解决方案1】:

您的所有类都不会覆盖.ToString(),因此当您调用Console.Write(sh[i] + " ") 时,它只是打印对象的类型名称。 (不是“随机文本”。)

您需要做以下两件事之一:

  1. 在您的类上定义 .ToString() 覆盖(最简单,推荐);
  2. 使shape.display()成为virtual方法(virtual void display())并在所有子类中使用override void display(),然后调用sh[i].display()而不是Console.Write(sh[i] + " ")

任何一个都可以解决您的问题。

当然,如果目标是在派生自shape 的所有类上创建getarea()getcirc(),那么您应该在shape 上标记它们virtual,然后在sub 上标记它们override -类。

更好的是,将shape 替换为interface (IShape),然后让所有子类实现接口。

【讨论】:

  • sh[i].display() 不起作用。形状是一个抽象类
  • @MohitShrivastava 已更新。
【解决方案2】:

使用Console.Write(sh[i] + " ") 会将对象引用输出到控制台,因为shape 是引用类型,或者简单地说,是对象,而不是原始/值类型。这就是您所说的“随机文本”的输出。

现在出现的问题是您要为自己的目的显示什么。如果你想简单地显示“这是一个正方形”或“这是一个圆形”,你将不得不使用调用显示函数 Console.Write(sh[i].display() + " ")

同样,您也可以致电sh[i].getcirc()sh[i].getarea() 打印周长和面积。

【讨论】:

  • 使用循环输入 15 个随机元素(圆形或矩形或正方形)后,我应该使用另一个循环使用三种方法打印每个元素的数据(面积、周长/周长和类型),但是它不允许我使用 sh[i].getcirc,因为 shape 是一个抽象类,
  • 您应该在class rect 之外声明class Programclass Square,例如class Circle。目前,它们相互嵌套,因此您的代码不起作用。
【解决方案3】:

我重构了您的代码并解决了这个问题。关键时刻:

  • 对方法使用抽象/覆盖词。
  • 为基类重写 ToString()
  • 遵循 C# 命名约定

代码如下:

using System;

namespace ConsoleApplication1
{
  abstract class Shape
  {
    public abstract string GetArea();
    public abstract string Display();
    public abstract string GetPerimeter();
    public override string ToString()
    {
      return Display() + ": " + GetArea() + ", " + GetPerimeter();
    }
  }

  class Circle : Shape
  {
    public Circle(double r)
    {
      this.r = r;
    }

    double r;

    public override string GetArea()
    {
      return "Circle area =" + Math.PI*Math.Pow(r, 2);
    }

    public override string GetPerimeter()
    {
      return "Circle circumference = " + 2*Math.PI*r;
    }

    public override string Display()
    {
      return "This is Circle";
    }
  }

  class Rect : Shape
  {
    public Rect(double x, double y)
    {
      this.x = x;
      this.y = y;
    }

    double x, y;

    public override string GetArea()
    {
      return "Rectangle area =" + x*y;
    }

    public override string GetPerimeter()
    {
      return "Circle circumference = " + (2*x + 2*y);
    }

    public override string Display()
    {
      return "This is Rectangle";
    }
  }

  class Square : Shape
  {
    public Square(double z)
    {
      this.z = z;
    }

    double z;

    public override string GetArea()
    {
      return "Square area =" + z*z;
    }

    public override string GetPerimeter()
    {
      return "Square perimeter = " + 4*z;
    }

    public override string Display()
    {
      return "This is Square";
    }

  }

  class Program
  {
    static void Main(string[] args)
    {
      var sh = new Shape[15];
      var rndm = new Random();

      for (var i = 0; i < 15; i++)
      {
        switch (rndm.Next(1, 4))
        {
          case 1:
            Console.WriteLine("Enter radius");
            sh[i] = new Circle(int.Parse(Console.ReadLine()));
            break;
          case 2:
            Console.WriteLine("Enter x and y lengths");
            double x = double.Parse(Console.ReadLine());
            double y = double.Parse(Console.ReadLine());
            sh[i] = new Rect(x, y);
            break;
          case 3:
            Console.WriteLine("Enter side length");
            sh[i] = new Square(int.Parse(Console.ReadLine()));
            break;
        }
      }

      foreach (var shape in sh)
        Console.WriteLine(shape);
      Console.ReadLine();
    }
  }
}

【讨论】:

    猜你喜欢
    • 2016-05-09
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2018-12-02
    相关资源
    最近更新 更多