如何从另一个类中访问一个类

本文关键字:一个 另一个 访问 | 更新日期: 2023-09-27 18:07:15

需要帮助访问帐户类。当我运行这个程序时,它用ATM类的WriteLine("欢迎/进入账户/退出")提示。然而,在我输入一个数字之后,命令窗口就关闭了。我不知道该怎么做。我还应该提一下,这是我第一次用C Sharp编程。另外,我不知道为什么人们会对我的问题投反对票,因为我是新来的。

账户类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    class Account //Within the Account class, we have balance, withdraw,and deposit
    {
        ////An account array to create 3 seperate accounts each with a default balance of $100.00. 
        //int[] myAccount = new int[3];
        Account[] account = new Account[3];

        public double balance;
        public void deposit(double n)
        {
            balance += n;
        }
        public void withdraw(double n)
        {
            balance -= n;
        }
        public void calcInterest(double n)
        {
            //Here is where we calculate the interest!
        }
        public void menu()
        {
            {
                {
                    int input = Convert.ToInt32(Console.ReadLine());
                    var currAccount = account[input]; // Not sure what this code is for. 

                    if (account[input] == null)
                    {
                        account[input] = new Account();
                        account[input].balance = 100; //Set initial balance to $100
                    }
                    if (input != 4)
                    {
                        Console.WriteLine("1) Deposit");
                        Console.WriteLine("2) Withdraw");
                        Console.WriteLine("3) Get Balance");
                        Console.WriteLine("4) Exit");
                        if(input == 1)
                            {
                            Console.WriteLine("How much would you like to deposit today?");
                            int moneyIn = Convert.ToInt32(Console.ReadLine());
                            account[input].deposit(moneyIn); //access the deposit method and increase balance by the amount entered by user.
                            Console.WriteLine("Here is your current balance:" + account[input].balance);
                        }
                            if(input == 2)
                            {
                            Console.WriteLine("How much would you like to withdraw today?");
                            int moneyOut = Convert.ToInt32(Console.ReadLine());
                            account[input].withdraw(moneyOut); //Access the withdraw method and decrease balance by the amount entered by user.
                            Console.WriteLine("Here is your current balance:" + account[input].balance);
                        }
                        if (input == 3)
                            {
                            Console.WriteLine("Here is your current balance:"+account[input].balance);
                            //return account[input].balance;
                        }
                        if (input == 4)
                        {
                            //I want to exit the application here. 
                        }                  
                     }
                 }
            }
        }
    }
}
ATM类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    class Atm //With the atm class we will have the atm menu
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome!");
            Console.WriteLine("Please enter your account number (1-3 or '4' to exit.");
            int input = Convert.ToInt32(Console.ReadLine());
            {

                if (input >= 1 && input <= 3)
                {
                    Console.WriteLine("You have entered " + input);
                    Console.ReadLine();
                    //ConsoleApplication3.Account[input]; // How do I access the account here? 
                }
                else if (input == 4)
                {
                    Console.WriteLine("Goodbye.");
                    //Exit Application
                }
              }
           }
        }
    }

程序类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
    class Program
    {
      //Not really sure what this is for at the moment, or if it is even needed. 
    }
}

如何从另一个类中访问一个类

首先,不要把控制台。WriteLine和Console。ReadLine逻辑在Account类中。也就是说,用户交互代码应该有自己的位置,也许在Atm类中,但是在它们自己的方法中。

在Atm类中,为Accounts创建引用,如下所示

Accounts[] accounts = new Accounts[3]

减少主菜单输入的重复。如果输入在1 &安培之间;3、直接调用

Console.WriteLine("You have selected account "+input);

那么输入处理将变成:

int input = Convert.ToInt32(Console.ReadLine());
{
   if (input >=1 && input <= 3)
   {
      Console.WriteLine("You have entered "+input);
      Console.ReadLine();
      //Access Account 
   }                    
   else if (input == 4)
   {
      Console.WriteLine("Goodbye.");
      //Exit Application
   }
}

然后像这样访问关联的帐户

if (accounts[input]==null)
{
   accounts[input] = new Account();
}
currAccount = accounts[input];
//either call the account interaction menu or write it here

同时,提款方法需要确保有足够的余额(或者如果允许透支,则最大透支金额)。

如果你想访问Account对象,首先实例化它并给它一个名字,这里的对象叫做theAcc。

Account theAcc = new Account();

如果你想访问对象的属性,例如,balance,你可以这样做:

Console.Writeline("The account's balance is: " + theAcc.Balance);