C#学习教程:错误“Thenon-staticfield,methodorproperty'QuickSharp.CokeMachine.TotalInsertedCoins'requiresanobjectreference”对象引用”编译时出现以下错误:CS0120:非静态字段、方法或属性“QuickSharp.CokeMachine.TotalInsertedCoins”需要对象引用这是因为我正在尝试使用另一个类的TotalInsertedCoins变量(CokeForm).我怎么解决这个问题?如果我想在我的CokeForm方法中添加按钮,我会遇到同样的问题。我的代码:usingSystem;使用System.Windows.Forms;使用系统绘图;命名空间QuickSharp{公共类CokeMachine{按钮可乐;按钮精灵;按钮芬达;按钮红牛;纽扣汁;纽扣水;双CokeCost=1.00;双SpriteCost=1.00;双FantaCost=1.00;双红牛成本=2.50;双JuiceCost=2.00;双WaterCost=0.50;按钮硬币1;按钮Coin2;按钮硬币3;纽扣硬币4;按钮Coin5;按钮Coin6;双CoinAmount1=0.05;双CoinAmount2=0.10;双CoinAmount3=0.20;双CoinAmount4=0.50;doubleCoinAmount5=1.00;双倍CoinAmount6=2.00;publicdoubleTotalInsertedCoins=0;//饮料按钮的事件处理程序publicvoidDrinkBut??tonsEvents(){Coke.Click+=newEventHandler(Coke_ClickHandler);Sprite.Click+=newEventHandler(Sprite_ClickHandler);Fanta.Click+=newEventHandler(Fanta_ClickHandler);RedBull.Click+=newEventHandler(RedBull_ClickHandler);Juice.Click+=新的事件处理器(Juice_ClickHandler);Water.Click+=newEventHandler(Water_ClickHandler);}//饮料按钮-点击事件处理程序}else{MessageBox.Show("您没有投入足够的钱来购买这种饮料。","警告");}}publicvoidSprite_ClickHandler(objectsender,EventArgse){if(TotalInsertedCoins>=SpriteCost){DispenseDrink("Sprite",SpriteCost);}else{MessageBox.Show("您没有投入足够的钱来购买这种饮料。","警告");}}publicvoidFanta_ClickHandler(objectsender,EventArgse){if(TotalInsertedCoins>=FantaCost){DispenseDrink("Fanta",FantaCost);}else{MessageBox.Show("您没有投入足够的钱来购买这种饮料。","警告");}}publicvoidRedBull_ClickHandler(objectsender,EventArgse){if(TotalInsertedCoins>=RedBullCost){DispenseDrink("红牛",RedBullCost);}else{MessageBox.Show("您没有投入足够的钱来购买这种饮料。","警告");}}publicvoidJuice_ClickHandler(objectsender,EventArgse){if(TotalInsertedCoins>=JuiceCost){DispenseDrink("橙汁",JuiceCost);}else{MessageBox.Show("您没有投入足够的钱来购买这种饮料。","警告");}}publicvoidWater_ClickHandler(objectsender,EventArgse){if(TotalInsertedCoins>=WaterCost){DispenseDrink("Water",WaterCost);}else{MessageBox.Show("您没有投入足够的钱来购买这种饮料。","警告");}}//金钱按钮的事件处理程序publicvoidMoneyButtonEvents(){Coin1.Click+=newEventHandler(Coin1_ClickHandler);Coin2.Click+=newEventHandler(Coin2_ClickHandler);Coin3.Click+=newEventHandler(Coin3_ClickHandler);Coin4.Click+=newEventHandler(Coin4_ClickHandler);Coin5.Click+=newEventHandler(Coin5_ClickHandler);Coin6.Click+=newEventHandler(Coin6_ClickHandler);}//Moneybuttons-Click事件处理程序publicvoidCoin1_ClickHandler(objectsender,EventArgse){TotalInsertedCoins+=CoinAmount1;}publicvoidCoin2_ClickHandler(objectsender,EventArgse){TotalInsertedCoins+=CoinAmount2;}publicvoidCoin3_ClickHandler(objectsender,EventArgse){TotalInsertedCoins+=CoinAmount3;}publicvoidCoin4_ClickHandler(objectsender,EventArgse){TotalInsertedCoins+=CoinAmount4;}publicvoidCoin5_ClickHandler(objectsender,EventArgse){TotalInsertedCoins+=CoinAmount5;}publicvoidCoin6_ClickHandler(objectsender,EventArgse){TotalInsertedCoins+=CoinAmount6;}privatevoidDispenseDrink(stringdrink,doublecost){if(TotalInsertedCoins-cost==0.0){MessageBox.Show("享受你的"+drink+"!");TotalInsertedCoins=0.0;}else{MessageBox.Show("享受你的"+饮料+"!这是你的零钱:"+(TotalInsertedCoins-cost));TotalInsertedCoins=0.0;}}}公共课ssCokeForm:Form{publicCokeForm(){//机器的一般方面this.Text="ColaMachine";this.Size=newSize(450,500);标头;标题=新标签();Header.Text="可口可乐机";Header.Font=newFont("宋体",Header.Font.Size+5);Header.ForeColor=Color.DarkRed;Header.Location=newPoint(132,20);Header.AutoSize=true;this.Controls.Add(Header);文本框TextBox1;TextBox1=newTextBox();TextBox1.BackColor=Color.Black;TextBox1.ForeColor=Color.Red;TextBox1.Font=newFont("宋体",TextBox1.Font.Size+3);TextBox1.ReadOnly=true;TextBox1.Size=newSize(210,300);TextBox1.Location=newPoint(112,50);//TextBox1.SelectionStart=TextBox1.Text.Length;//TextBox1.ScrollToCaret();//TextBox1.Refresh();if(CokeMachine.TotalInsertedCoins==0.00){TextBox1.Text="在这里买冰镇饮料!";}else{TextBox1.Text="插入的硬币:"+CokeMachine.TotalInsertedCoins;}this.Controls.Add(TextBox1);//米机器标签钱的一个方面;钱=新标签();Money.Text="在这里投币:";Money.Location=newPoint(20,100);this.Controls.Add(金钱);//Moneybuttonswillbehere//DrinkaspectofmachineLabelDrinks;饮料=新标签();Drinks.Text="选择你的饮料:";Drinks.Location=newPoint(315,100);Drinks.AutoSize=true;这。控制。添加(饮料);//饮料按钮会在这里}}publicclassTest{publicstaticvoidMain(){CokeFormColaForm;ColaForm=newCokeForm();应用程序运行(ColaForm);我该如何解决这个问题?关于CS0120错误的其他问题不会让我有任何进一步的问题你永远不会创建CokeMachine的实例:if(CokeMachine.TotalInsertedCoins==0.00){TextBox1.Text="BuyYourIceColdDrinksHere!";}else{文本框1.Text="插入的硬币:"+CokeMachine.TotalInsertedCoins;在表单中创建一个CokeMachine实例,然后使用它。以上就是C#学习教程的全部内容:报错“Non-staticfield,methodorproperty'QuickSharp.CokeMachine.TotalInsertedCoins'requiresanobjectreference”。跟随—...CokeMachinemachine=newCokeMachine();...if(machine.TotalInstertedCoins==0.00){....}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
