[英]Working with getter and setter in c#
我是C#的新手,想知道在處理get和set時我做錯了什么。
我有一個自動售貨機,當我輸入想要的飲料的數字時,它會從總數中減去。
private int _coke = 2;
public int Beer = 20;
public int LemonLime = 20;
public int Grape = 20;
public int CreamSoda = 20;
public bool AnohterBevrage;
問題在於,即使達到0,它仍會繼續減去。好吧,機器中不能剩下-1的可樂。 所以,我嘗試了這個。
public int Coke
{
get => _coke;
set
{
if (_coke == 0)
{
_coke = value;
Console.WriteLine("No more Coke Left!");
}
}
}
但這是行不通的,所以我不確定在哪里卡住。 我不確定數學功能是否與此處相關。
如果缺少什么,請告訴我。 我會嘗試調整。 這個getter和setter使我都感到困惑。
編輯:添加功能
public void Math()
{
var input = Console.ReadKey();
while (input.Key == ConsoleKey.D1)
{
do
{
_coke--;
Console.WriteLine("\nTotal Coke Left: " + Coke);
Console.Write("\nWould You like Another Bevrage ?y/n: ");
if (Console.ReadLine() == "y")
{
AnohterBevrage = true;
Content();
Math();
}
else
{
AnohterBevrage = false;
}
break;
} while (AnohterBevrage == true);
}
}
if (_coke == 0)
您正在檢查當前值。
這意味着您只能設置當前為0
的屬性。
我猜你想要這段代碼,如果_coke
小於0
顯示消息,否則減去_coke
值。
public int Coke
{
get { return _coke; }
set
{
if (_coke <= 0)
{
Console.WriteLine("No more Coke Left!");
}
else
{
_coke = value;
}
}
}
當您開始嘗試做某事時,您需要建立一個正在做的事情的模型。 使用getters / setters作為UI並不是一個很好的設計(如@Rufus L所指出的)。 取而代之的是,將一台可樂機器想象成腦海。 有各種各樣的飲料架(可以進貨),然后是一個機械系統,該機械系統實現了一個用戶界面,該界面允許某人領取飲料並將其交付。 您應該將設計分為這兩個部分。
設計需要工作的一個提示是,您的主要功能(即完成所有工作)被命名為“ Math”。 如果要在兩個月內查看這段代碼,您會看到“ Math”,而沒有任何線索可以代表可樂機器。
我寫了一台可以滿足您需求的快速煉焦機(嗯,我認為可以)。 它使用相同的思維模型,一組飲料架和一個控制器。 我使用一個簡單的類來完成此任務,該類代表一堆飲料和一個用控制台應用程序的主例程編寫的控制器(因此,所有內容都是靜態的)。
RackOfBeverage類(它包含從未調用的補貨方法):
class RackOfBeverages
{
public string Label { get; }
public int Count { get; private set; }
public RackOfBeverages(string label, int initialCount)
{
Label = label;
Count = initialCount;
}
public void Restock(int howMany)
{
Count += howMany;
}
public bool Dispense()
{
if (Count > 0)
{
--Count;
return true;
}
else
{
return false;
}
}
}
它有一個標簽(表明是哪種飲料),庫存/數量和Dispense
方法。 如果沒有剩余飲料,分配呼叫將返回false。
然后,我編寫了一個簡單的控制器(作為控制台應用程序中的Main方法以及放在Program類中的其他一些內容):
class Program
{
private static readonly Dictionary<string, RackOfBeverages> Beverages =
new Dictionary<string, RackOfBeverages>
{
{"COKE", new RackOfBeverages("Coke", 2)},
{"SPRITE", new RackOfBeverages("Sprite", 20)},
//etc.
};
static void Main(string[] args)
{
Console.WriteLine("Welcome to the machine\r\nType a selection (type \"Exit\" to quit)");
while (true)
{
var selection = Console.ReadLine();
//did the user enter the name of a drink
if (Beverages.Keys.Contains(selection, StringComparer.OrdinalIgnoreCase))
{
var beverage = Beverages[selection.ToUpper()];
//was there enough to dispense a drink
if (beverage.Dispense())
{
Console.WriteLine($"Here's your {beverage.Label}");
}
else
{
Console.WriteLine($"Sorry, no {beverage.Label} for you");
}
}
//or, perhaps, the user chose to exit the app
else if (selection.ToUpper() == "EXIT")
{
Console.WriteLine("Exiting");
break;
}
//finally, if the user didn't enter anything I understand
//let him/her know and then let him/her try again
else
{
Console.WriteLine("Pick a valid selection");
}
}
}
}
分離應用程序中的“問題”很重要。 在這里,UI與飲料存儲區完全分開。 如果要添加其他飲料,只需將其添加到頂部的“詞典”中,UI將繼續對您添加的舊飲料和新飲料起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.