编译时错误 - 无法使用实例引用访问成员

Che*_*ire 1 c# class instance

我是一个新尝试理解类和类如何工作的人.我正在构建一个小型控制台程序,我正在处理我的'class.cs'文件,我将其命名为'LineItem.cs',因为它将处理我试图让我的控制台应用程序生成的收据上的订单项.

问题:无法使用实例引用访问成员'A070_ _CashRegister.Program.receipt()'; 用类型名称来限定它.(错误行:#21 /列#13)

当我输入'this.color = price;我认为我在#21行上做过这个.

码:

using System;
namespace a070___Classes___CashRegister
{
  class LineItem     // Class name is singular
                     // receipt might be better name for this class?
  {
    /// Class Attributes
    private String product;     // constructor attribute
    private String description;
    private String color;
    private double price;
    private Boolean isAvailable;

    // constructor called to make object => LineItem
    public LineItem(String product, String description, String color, int price, Boolean isAvailable)
    {
        this.product = product;
        this.description = description;
        this.color = color;
        this.price = price;
        this.isAvailable = isAvailable;// might want to do an availability check
    }

    //Getters
    public String GetProduct() {return product;}
    public String GetDescription(){return description;}//Send description
    public String GetColor() {return color;}

    //Setter
    public void SetColor(string color)//we might want to see it in other colors if is option
    { this.color = color; } //changes object color 
  }
}
Run Code Online (Sandbox Code Playgroud)

将调用该类的主文件:

using System;

namespace a070___Classes___CashRegister
{
  class Program
  {
    static void receipt()
    { 
    //stuff goes here - we call various instances of the class to generate some receipts
    }

    static void Main(string[] args)
    {
        //Program CashRegister = new Program();
        //CashRegister.receipt();

        //Program CashRegister = new Program();
        //CashRegister.receipt();

        receipt();// Don't need to instantiate Program, console applications framework will find the static function Main
        //unless changed your project properties.
        //Since reciept is member od Program and static too, you can just call it directly, without qualification.
    }
  }
} 
Run Code Online (Sandbox Code Playgroud)

Jod*_*ell 5

Program CashRegister = new Program();
CashRegister.receipt();
Run Code Online (Sandbox Code Playgroud)

应该

Program.receipt();
Run Code Online (Sandbox Code Playgroud)

要不就

receipt();
Run Code Online (Sandbox Code Playgroud)

您不需要实例化Program,使用控制台应用程序,框架将找到static function Main(...并通过魔术调用它,除非您已更改项目属性.

既然receipt是一个成员Programstatic也,你可以直接调用它,没有资格.


receipt()函数是,static但您正在尝试从实例调用它.

你没有显示receipt声明的位置或你在哪里调用它所以我无法提供更多帮助.

也许你有一行代码,其中有一个表达式,如,

... this.receipt() ...
Run Code Online (Sandbox Code Playgroud)

要么

... yourInstance.receipt() ...
Run Code Online (Sandbox Code Playgroud)

但应该是,

... Type.receipt() ...
Run Code Online (Sandbox Code Playgroud)