C# 这两种实例方法之间有什么区别?

C# 这两种实例方法之间有什么区别?,c#,instance,C#,Instance,这两种实例方法之间有什么区别 public class Food { public int apples; public int oranges; public int bananas; // Constructor #1 public Food(int a, int o, int b) { apples = a; oranges = o; bananas = b; } // Is t

这两种实例方法之间有什么区别

public class Food
{
    public int apples;
    public int oranges;
    public int bananas;

    // Constructor #1
    public Food(int a, int o, int b) 
    { 
        apples = a;
        oranges = o;
        bananas = b;
    }

// Is this an instance

public Food myFood = new Food(5, 8, 1);

// Or this 

Food.myfood(5, 8, 1)

我更有经验的朋友说,后者是一个例子,而不是第一个。

我猜你忘了关门

public class Food
{
    public int apples;
    public int oranges;
    public int bananas;

    // Constructor #1
    public Food(int a, int o, int b) 
    { 
        apples = a;
        oranges = o;
        bananas = b;
    }

} // <----  you were missing this I assume

// If the following is in your main method, then myFood is an instance
// of the Food class. 
// you can do myFood.apples = 4; // very bad btw, having public variables.
public Food myFood = new Food(5, 8, 1);

// Not even sure what this is ... because you don't have a myFood on Food.
// If you would have a static property on your Food class (Yummy), then you
// could do something like Food.Yummy = what_ever_the_type_is ... 
Food.myfood(5, 8, 1)
公共级食品
{
公众参与;
公共橙子;
公共设施;
//构造函数#1
公共食品(内部a、内部o、内部b)
{ 
苹果=a;
橙子=o;
香蕉=b;
}

}//我觉得人们甚至都没有阅读这个问题。 答:第一个是创建一个新的食品实例(前者)

第二个错误设置了该值。在f之后

Food myFood = new Food(3,4,5);
Console.WriteLine(myfood.Oranges) // prints 4;
myFood.Oranges = 3; // set the value of oranges for that object to 3
Console.WriteLine(myFood.Oranges); // prints 3;

我有一个朋友熟悉5种语言,他说阶梯就是你的例子。我只是看看我是对的还是他。谢谢!很高兴你真的回答了这个问题:)没问题,先生。不要担心被否决。仇恨者会憎恨的。他们不读/不懂,所以他们认为这是问题所在。这是一个完全合理的问题。
Food myFood = new Food(3,4,5);
Console.WriteLine(myfood.Oranges) // prints 4;
myFood.Oranges = 3; // set the value of oranges for that object to 3
Console.WriteLine(myFood.Oranges); // prints 3;