[英]how do i change the value of an object
這是我的主要方法:
string input = Console.ReadLine(); // 2006 2000 false 1000 500 Windows
List<string> inputs = input.Split(" ").ToList();
Computer one = new Computer(int.Parse(inputs[0]), int.Parse(inputs[1]), bool.Parse(inputs[2]), double.Parse(inputs[3]), double.Parse(inputs[4]), inputs[5]);
one.Print(); // 2006, 2000, false, 1000, 500, Windows
input = Console.ReadLine(); //changeOperatingSystem
if (input == "changeOperatingSystem")
{
string newOperationSystem = Console.ReadLine(); //Linux
Computer two = new Computer(newOperationSystem);
}
one.Print(); //2006, 2000, false, 1000, 500, Linux
“//”標記輸入和 output 的樣子。 這是我的 Computer.cs:
private int years;
private int price;
private bool isNotebook;
private double hardDiskMemory;
private double freeMemory;
private string operationSystem;
private string newOperationSystem;
public Computer(int years, int price, bool isNotebook, double hardDiskMemory, double freeMemory, string operationSystem)
{
Years = years;
Price = price;
IsNotebook = isNotebook;
HardDiskMemory = hardDiskMemory;
FreeMemory = freeMemory;
OperationSystem = operationSystem;
}
public Computer(string newOperationSystem)
{
OperationSystem = newOperationSystem;
}
public int Years
{
get { return years; }
set
{
years = value;
}
}
public int Price
{
get { return price; }
set
{
price = value;
}
}
public bool IsNotebook
{
get { return isNotebook; }
set
{
isNotebook = value;
}
}
public double HardDiskMemory
{
get { return hardDiskMemory; }
set
{
hardDiskMemory = value;
}
}
public double FreeMemory
{
get { return freeMemory; }
set
{
freeMemory = value;
}
}
public string OperationSystem
{
get { return operationSystem; }
set
{
operationSystem = value;
}
}
public string NewOperationSystem
{
get { return NewOperationSystem; }
set
{
newOperationSystem = value;
}
}
public void Print()
{
Console.WriteLine($"{years}, {price}, {isNotebook}, {hardDiskMemory}, {FreeMemory}, {operationSystem}");
}
我想讓這個程序做的是,只要用戶提交的輸入是"changeOperationSystem"
,程序就會給用戶另一個輸入,讓他們為 pc 提交新的操作系統。 然后使用構造函數,Computer.Cs 接收字符串newOperationSystem
,然后它應該替換值operationSystem
,就像我在構造函數中嘗試做的那樣: OperationSystem = newOperationSystem
,但它不起作用。 我還在上課,所以不要 go 對我太苛刻。
在我看來,您正在嘗試更改 class 的一個屬性。您所要做的就是使用屬性設置方法
one.OperationSystem = newOperationSystem;
並完全刪除屬性NewOperationSystem
。
這是使用構造函數實例化 class 然后更改其中一個屬性的基本框架代碼
public class Foo
{
private int _a;
private string _b;
private readonly float _c;
public Foo(int a, string b, float c)
{
_a = a;
_b = b;
_c = c;
}
public int A { get => _a; set => _a = value; }
public string B { get => _b; set => _b = value; }
public float C => _c;
public override string ToString()
{
return $"{_a}, {_b}, {_c}";
}
}
class Program
{
static void Main(string[] args)
{
Foo one = new Foo(100, "Philip", 0.75f);
Console.WriteLine(one);
//100, Philip, 0.75
one.B = "Spencer";
Console.WriteLine(one);
//100, Spencer, 0.75
}
}
上面代碼的另一個特點是,如果您可以利用在Console.WriteLine()
或任何其他操作期間自動調用的內置.ToString()
,則無需創建.Print()
方法需要從我的 class 輸入字符串。
最后,我添加了一個只讀屬性C
來證明這種設計是可行的。 你將無法寫出one.C = 0.25f;
因為該屬性是只讀的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.