C# 不是所有函数都必须实例化吗?

C# 不是所有函数都必须实例化吗?,c#,C#,运行此C代码时,结果是: 长度:4.5 宽度:3.5 面积:15.75 我的新手问题是: 如果您删除了r.Acceptdetails;或r.显示;然后它们不实例化,输出也不符合预期 那么,为什么不必实例化GetArea呢 作为一名新手,我试图理解为什么有些函数必须实例化,而有些函数不能实例化。首先,你一开始就错了。您不实例化函数,而是实例化类对象。在您的代码中,创建的唯一实例是新矩形;矩形类的。在需要时调用该函数 因此,在代码中,您自己调用了两个实例函数 using System; namesp

运行此C代码时,结果是:

长度:4.5 宽度:3.5 面积:15.75

我的新手问题是:

如果您删除了r.Acceptdetails;或r.显示;然后它们不实例化,输出也不符合预期

那么,为什么不必实例化GetArea呢


作为一名新手,我试图理解为什么有些函数必须实例化,而有些函数不能实例化。

首先,你一开始就错了。您不实例化函数,而是实例化类对象。在您的代码中,创建的唯一实例是新矩形;矩形类的。在需要时调用该函数

因此,在代码中,您自己调用了两个实例函数

using System;
namespace RectangleApplication
{
    class Rectangle
    {
        // member variables
        double length;
        double width;
        public void Acceptdetails()
        {
            length = 4.5;
            width = 3.5;
        }

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

        public void Display()
        {
            Console.WriteLine("Length: {0}", length);
            Console.WriteLine("Width: {0}", width);
            Console.WriteLine("Area: {0}", GetArea());
        }
    }

    class ExecuteRectangle
    {
        static void Main(string[] args)
        {
            Rectangle r = new Rectangle();
            r.Acceptdetails();
            r.Display();
            Console.ReadLine();
        }
    }
}
但另一个函数正在显示函数中调用;在代码本身中

r.Acceptdetails();
r.Display();
从这一行可以清楚地看到,您确实调用了函数GetArea。。。不仅在主函数中,而是在另一个函数中。如果您想自己调用该函数,可以将代码更改为

Console.WriteLine("Area: {0}", GetArea());
备用代码 同样如前所述,您也可以在构造函数中使用参数。所以你的构造函数可以是这样的

public void Display()
{
   Console.WriteLine("Length: {0}", length);
   Console.WriteLine("Width: {0}", width);
}

// inside the Main function
Rectangle r = new Rectangle();
r.Acceptdetails();
r.Display();
r.GetArea()
Console.ReadLine();
保留其余的函数,但删除不必要的Acceptdetails函数,代码如下所示

// pass the parameters
public Rectangle(double length, double width) {
    // fill the values
    this.length = length;
    this.width = width;
}

我相信函数实例化是指函数调用

如果您删除了r.Acceptdetails;或r.显示;然后他们 如果不实例化,则输出不符合预期

AcceptDetails方法正在为长度和宽度字段赋值。在代码中没有其他地方可以为它们赋值。因此,当您省略对AcceptDetails的调用时,将不会得到所需的结果

Display方法调用实际执行计算的GetArea方法

更重要的是,你做错了。在对象实例化时或在构造函数中赋值。 定义一个构造函数,如:

using System;

namespace RectangleApplication
{
   class Rectangle 
   {
      // member variables
      double length;
      double width;

      public Rectangle(double length, double width) 
      {
          this.length = length;
          this.width = width;
      }

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

      public void Display()
      {
         Console.WriteLine("Length: {0}", length);
         Console.WriteLine("Width: {0}", width);
      }
   }

   class ExecuteRectangle 
   {
      static void Main(string[] args) 
      {
         Rectangle r = new Rectangle();
         r.Display();
         Console.WriteLine("Area: {0}", r.GetArea());
         Console.ReadLine(); 
      }
   }
}
然后在Main方法中调用它,如:

public Rectangle(double lengthParameter, double widthParamter)
{
    this.length = lengthParameter;
    this.width = widthParamter;
}

作为旁注,请按照,为您的类字段/属性

因为你就是这样写代码的。你还想听到什么?GetArea是类的本地方法,你在类内调用它,如果你从类外调用它,那么你需要使用instanceGetArea调用它,因为它是从实例内调用的,所以这意味着你在调用this.GetArea
Rectangle r = new Rectangle(4.5, 3.5);
r.Display();