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

时间:2016-09-16 05:06:40

标签: c# arrays

需要协助才能访问帐户类。当我运行程序时,它会提示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. 
    }
}

2 个答案:

答案 0 :(得分:1)

首先,不要将Console.WriteLine和Console.ReadLine逻辑放在Account类中。也就是说,用户交互代码应该有自己的位置,可能在Atm类中,但在他们自己的方法中。

在Atm类中,为Accounts创建一个引用,例如

# http://www.wildml.com/2016/08/rnns-in-tensorflow-a-practical-guide-and-undocumented-features/
def make_example(input_sequence, output_sequence):
    """
    Makes a single example from Python lists that follows the
    format of tf.train.SequenceExample.
    """

    example_sequence = tf.train.SequenceExample()

    # 3D length
    sequence_length = sum([len(word) for word in input_sequence])
    example_sequence.context.feature["length"].int64_list.value.append(sequence_length)

    input_characters = example_sequence.feature_lists.feature_list["input_characters"]
    output_characters = example_sequence.feature_lists.feature_list["output_characters"]

    for input_character, output_character in izip_longest(input_sequence,
                                                          output_sequence):

        # Extend seems to work, therefore it replaces append.
        if input_sequence is not None:
            input_characters.feature.add().int64_list.value.extend(input_character)

        if output_characters is not None:
            output_characters.feature.add().int64_list.value.extend(output_character)

    return example_sequence

减少主菜单输入的重复次数。如果输入介于1和1之间。 3,只需致电

Accounts[] accounts = new Accounts[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
   }
}

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

答案 1 :(得分:0)

如果您想访问Account对象,首先将其实例化并为其命名,此处该对象称为TheAcc。

map?country=US

如果您想要访问该对象的属性,例如余额,您可以执行以下操作:

Account theAcc = new Account();
相关问题