【问题标题】:How do I introduce an initialization constructor? C# CS0236如何引入初始化构造函数? C# CS0236
【发布时间】:2017-04-13 05:14:10
【问题描述】:

我遇到了多个错误,但我不知道为什么。错误是在GetArea 方法之后引入的。

namespace Lesson02
{
    class Rectangle
    {
        static void Main(string[] args)
        {

        }

        private double length;
        private double width;
        public Rectangle(double l, double w)

        {
            length = l;
            width = w;

        }

        public double GetArea()

        {
            return length * width;
        }

       public Rectangle rect = new Rectangle(10.0, 20.0);
       double area = rect.GetArea();
       Console.WriteLine("Area of Rectagle: {0}", area);

【问题讨论】:

  • Console.WriteLine("Area of Rectagle: {0}", area); 应该是方法中
  • 请编写您的完整类或其中的可编译部分。并提及你得到了什么exat错误。
  • 我确实把所有的东西都写完了,而且我确实在问题的标题中包含了我得到的错误。感谢您的回答,但我几乎可以肯定他们已经多次看到 Console.WriteLine 的“外部”方法。

标签: c# constructor access-modifiers


【解决方案1】:

你不能只执行

Console.WriteLine("Area of Rectagle: {0}", area);

在类范围内,就好像它是声明。将其移至Main 方法:

namespace Lesson02
{
    class Rectangle
    {
        // Method, here we execute
        static void Main(string[] args)
        { 
            // Executions are within the method
            Rectangle rect = new Rectangle(10.0, 20.0);
            double area = rect.GetArea();
            Console.WriteLine("Area of Rectagle: {0}", area);
        }

        // Declarations
        private double length;
        private double width;

        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }

        public double GetArea()
        {
            return length * width;
        }
    }
}

【讨论】:

  • 感谢您的帮助。
【解决方案2】:

如评论中所述,您将类主体与程序代码混合在一起。将所有内容都放在一个类中也是一个坏主意。

你的 Rectangle 类应该是独立的:

public class Rectangle
{
    private double length;
    private double width;

    public Rectangle(double l, double w)
    {
        length = l;
        width = w;

    }

    public double GetArea()
    {
        return length * width;
    }
}

和你的程序代码分开:

public class Program
{
    static void Main(string[] args)
    {
        Rectangle rect = new Rectangle(10.0, 20.0);
        double area = rect.GetArea();
        Console.WriteLine("Area of Rectagle: {0}", area);
    }
}

【讨论】:

  • 谢谢米萨。我真的很感激。
【解决方案3】:

要么将类 Rectangle 设为公共,否则更改 公共矩形矩形 = 新矩形(10.0,20.0); as 矩形 rect = new Rectangle(10.0, 20.0);

    public class Rectangle
    {

        private double length;
        private double width;
        public Rectangle(double l, double w)
        {
            length = l;
            width = w;
        }

        public double GetArea()
        {
            return length * width;
        }

    }
    static void Main(string[] args)
    {
         Rectangle rect = new Rectangle(10.0, 20.0);
        double area = rect.GetArea();
        Console.WriteLine("Area of Rectagle: {0}", area);
    }

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 2021-06-30
    • 2020-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多