简体   繁体   English

如何从C#中的对象获取价值

[英]How to get value from object in C#

I'm writing a simple ATM program in C# and it is suppose to be Object Oriented. 我正在用C#编写一个简单的ATM程序,它应该是面向对象的。 I created a public class ATM and inside it a another public class ACC (account). 我创建了一个公共类ATM,并在其中创建了另一个公共类ACC(帐户)。 How can i create method inside ATM class to write how much money is in ATM.ACC.ACCSaldo? 我如何在ATM类中创建方法来写ATM.ACC.ACCSaldo中有多少钱?

public class ACC
{
    public int ACCNr;       //accaunt number
    public int ACCPIN;      //PIN number
    public double ACCSaldo; //How much money is in account

    //constr
    public ACC(int p, int p2, double p3)
    {
        ACCNr = p;
        ACCPIN = p2;
        ACCSaldo = p3;
    }
}

My main part of code looks like this: 我的主要代码部分如下所示:

static void Main(string[] args)
{
    ATM ATM01 = new ATM();

    ATM.ACC acc1 = new ATM.ACC(3333, 1234, 3000.53);

    acc1.ACCNr = ATM01.czytnik();

    Console.WriteLine("ACCNr: {0}", acc1.ACCNr);
    Console.WriteLine("PIN: {0}", acc1.ACCPIN);
    Console.WriteLine("Saldo: {0}", acc1.ACCSaldo);

    ATM.tstPIN tstPIN1 = new ATM.tstPIN(acc1.ACCNr, acc1.ACCPIN ,acc1.ACCSaldo);
    tstPIN1.test();

    ATM.menu();

    // Keep the console window open in debug mode.
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
}   

If there is only one Account in the ATM, you can declare a ACC instance (eg _acc) in the ATM; 如果ATM中只有一个帐户,则可以在ATM中声明一个ACC实例(例如_acc);否则,您可以在ATM中声明一个ACC实例。 You need to initialise in your main method, then you can do sth like: 您需要在主方法中进行初始化,然后可以执行以下操作:

public void DisplayAccountDetails()
{
   Console.Write(String.Format("Account Balance:{0}", _acc.ACCSaldo));
}

I hope this helps. 我希望这有帮助。

You could add get and set properties. 您可以添加get和set属性。 public double ACCSaldo{ get; 公开双重ACCSaldo {get; set; 组; } }

Assuming there is only one account in the ATM class and it looks similar to the follow: 假设在ATM类中只有一个帐户,它看起来类似于以下内容:

public class ATM
{
    public ACC Acc;
}

You can add a method inside the ATM class like the following: 您可以在ATM类中添加如下方法:

public void WriteSaldo()
{
    Console.WriteLine("Saldo: {0}", Acc.ACCSaldo);
}

Then in your Main method you can use ATM01.WriteSaldo(); 然后,在您的Main方法中,可以使用ATM01.WriteSaldo();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM