gpt4 book ai didi

c# - 解析类变量?

转载 作者:行者123 更新时间:2023-11-30 13:25:46 26 4
gpt4 key购买 nike

我对 C# 语言还很陌生,因为我几周前才开始使用它,我遇到了一个关于类的简单问题。我坐了整整 30 分钟寻找答案,但就是想不通怎么办要解决这个问题。到目前为止,这是我的代码:

class Program
{
static void Main(string[] args)
{
Plyta p1;
Plyta p2;
p1 = new Plyta(p1.ImtiIlgi(), p1.ImtiAuksti());


p1.ImtiIlgi();
Console.Write("\niveskite plytos ilgi - ");
p1.PlytosIlgis = int.Parse(Console.ReadLine());

p1.ImtiAuksti();
Console.Write("\niveskite plytos auksti - ");
p1.PlytosAukstis = int.Parse(Console.ReadLine());

和类文件:

class Plyta
{
private int ilgis,
aukstis;

public Plyta(int PlytosIlgis, int PlytosAukstis)
{
ilgis = PlytosIlgis;
aukstis = PlytosAukstis;
}


public int ImtiIlgi()
{
return ilgis;
}

public int ImtiAuksti()
{
return aukstis;
}
}

当我运行它时,它给出了以下错误:

'praktika.Plyta' does not contain a definition for 'PlytosIlgis' and no extension method 'PlytosIlgis' accepting a first argument of type 'praktika.Plyta' could be found (are you missing a using directive or an assembly reference?)

我假设我写了错误的变量来解析,但无论我想到什么都行不通,对初学者有什么帮助吗? :)

最佳答案

我认为您对方法的作用以及初始化对象的时间感到困惑。我怀疑你真的想做这样的事情:

class Program
{
static void Main(string[] args)
{
// Ask the user for the relevant data
Console.WriteLine("iveskite plytos ilgi - ");
int ilgis = int.Parse(Console.ReadLine());

Console.WriteLine("iveskite plytos auksti - ");
int aukstis = int.Parse(Console.ReadLine());

// Now we're in a position to create the object
Plyta p1 = new Plyta(ilgis, aukstis);

// And we can read the value back from the property
Console.WriteLine(p1.Ilgis);
}
}

class Plyta
{
// These are public, read-only automatically-implemented properties
public int Ilgis { get; }
public int Aukstis { get; }

public Plyta(int ilgis, int aukstis)
{
// Set the properties...
Ilgis = PlytosIlgis;
Aukstis = PlytosAukstis;
}
}

(这使用 C# 6 的只读自动实现属性。如果您不使用 C# 5,请告诉我。)

关于c# - 解析类变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39662155/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com