从另一个类访问方法 [英] Access method from another class

查看:76
本文介绍了从另一个类访问方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试从另一个类访问一个方法,但第二个想法我可以返回一个变量。



我这里有2个选项。我可以在我的User类GiveCash方法中调用myBank.Deposit(cash)方法,或者我可以通过使用该方法返回变量/值。



我认为,为了访问BankAccount类中的存款方法,我必须初始化我的意思是创建该类的一个对象,因为它已经在我的主类/代码中已经完成,所以没有任何意义。



所以,我只是在寻求一些建议,这里最好的事情是什么,为什么?



  //  开发者:xxx xxx  

使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 MyApplication
{
class Program
{
静态 void Main( string [] args)
{
short choice;
十进制 money;
string 输入;

BankAccount myBank = new BankAccount( AIB BankAccount No.123 1000 );
用户me = 用户( Michal 200 );

me.Print();
myBank.Print();

Console.WriteLine( \ nOptions:);
Console.WriteLine( 1. - 存款);
Console.WriteLine( 2. - Withdraw);

Console.WriteLine( \ n请选择您的行动号码!);
input = Console.ReadLine();
choice = Convert.ToSByte(输入);

if (choice == 1
{
Console.Write( 存入值:);
input = Console.ReadLine();
money = Convert.ToDecimal(输入);

me.GiveCash(钱);
myBank.Deposit(money);

}
else
{
Console.Write( 提取值:);
input = Console.ReadLine();
money = Convert.ToDecimal(输入);

myBank.Withdraw(money);
}

Console.Read();
}
}
}

使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 MyApplication
{
class 用户
{
string name;
十进制现金;

public 用户(): 默认名称 0 ){}

public 用户(字符串名称,十进制现金)
{
name = Name;
现金=现金;
}

public decimal GiveCash( decimal value
{
if (现金< = 价值
{
cash - = ;

返回现金;
}
else
{
Console.WriteLine( DAMN!我口袋里没有钱!);
}
}

public void 打印( )
{
Console.WriteLine( Hello {0}你有{1}现金你的口袋。,名字,现金);
}
}
}



使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;

命名空间 MyApplication
{
class BankAccount
{
// 由于安全性,建议不要使用公共字段
string name;
十进制余额;

// 提供默认构造函数
public BankAccount(): this 默认名称 0 ){}

/ / 构造函数用于初始化数据字段,我们可以将变量传递给构造函数,但它不返回值
public BankAccount( string 名称,十进制余额)
{
余额=余额;
name = Name;
}

public void 提取( decimal value
{
balance - = value < /跨度>;
Console.WriteLine( 当前余额为{0}。,余额);
}

public void 存款( decimal value
{
余额+​​ = value < /跨度>;
Console.WriteLine( 当前余额为{0}。,余额);
}

public void 打印()
{
Console.WriteLine( 当前余额{0}为{1}。 ,名称,平衡);
}

// 不需要析构函数,它在垃圾收集器中自动完成
}
}





欢迎提出任何意见或建议。谢谢。

解决方案

由于存款在逻辑上是不可能的,没有账户存入,你需要有一个BankAccount类的实例准备接收钱。所以你拥有它的方式已经是正确的:

 myBank.Deposit(money); 



混乱当你试图通过用户存钱时,不是因为你必须创建该类的对象,而是因为你还没有在用户和账户之间建立链接。



想想看:当你(用户)用拳头掏出你辛苦赚来的钱时,他们首先要知道的是什么账户? - 如果只是因为(像我和其他许多人一样)你可能在同一家银行拥有多个账户:例如,支票和存款账户。



所以它不是'你想要 创建 和帐户的实例,而是你需要 找出 实例用户计划存入的账户:他可能同时将钱存入多个账户 - 50%在这里,20%在那里,30%其他在那里请!



下一个有趣的事情是,Michal应该允许在该帐户存款或取款吗? :笑:



所以想想你需要如何在Main方法中运行 - 弄清楚真实银行会发生什么,并尝试模仿它。


Hello I am trying to access a method from another class but on the second thought I could return a variable instead.

I have 2 options here. I can call "myBank.Deposit(cash)" method in my User class GiveCash method or I could simply return a variable/value by using the method.

I think, in order to access deposit method in BankAccount class I would have to initialize I mean create an object of that class and that wouldn't make any sense since its already done in my main class/code.

So, I am just seeking a little bit of advice what is the best thing to do here and why?

// Developed by : xxx xxx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            short choice;
            decimal money;
            string input;

            BankAccount myBank = new BankAccount("AIB BankAccount No.123", 1000);
            User me = new User("Michal", 200);

            me.Print();
            myBank.Print();

            Console.WriteLine("\nOptions:");
            Console.WriteLine("1. - Deposit");
            Console.WriteLine("2. - Withdraw");

            Console.WriteLine("\nPlease choose the number of your action!");
            input = Console.ReadLine();
            choice = Convert.ToSByte(input);

            if (choice == 1)
            {
                Console.Write("Deposit the value : ");
                input = Console.ReadLine();
                money = Convert.ToDecimal(input);

                me.GiveCash(money);
                myBank.Deposit(money);
                
            }
            else
            {
                Console.Write("Withdraw the value : ");
                input = Console.ReadLine();
                money = Convert.ToDecimal(input);

                myBank.Withdraw(money);
            }

            Console.Read();
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    class User
    {
        string name;
        decimal cash;

        public User() : this("Default name", 0) { }

        public User(string Name, decimal Cash)
        {
            name = Name;
            cash = Cash;
        }

        public decimal GiveCash(decimal value)
        {
            if (cash <= value)
            {
                cash -= value;

                return cash;
            }
            else
            {
                Console.WriteLine("DAMN! I dont have enought money in my pocket!");
            }
        }

        public void Print()
        {
            Console.WriteLine("Hello {0} you have {1} cash in your pocket.", name, cash);
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyApplication
{
    class BankAccount
    {
        // public fields are not recommended due to security
        string name;
        decimal balance;

        // providing default constructor
        public BankAccount() : this("default name", 0) { }

        // constructor is used to initialize data fields, we can pass variables to a constructor but it doesnt return a value
        public BankAccount(string Name, decimal Balance)
        {
            balance = Balance;
            name = Name;
        }

        public void Withdraw(decimal value)
        {
            balance -= value;
            Console.WriteLine("Current balance is {0}.", balance);
        }

        public void Deposit(decimal value)
        {
            balance += value;
            Console.WriteLine("Current balance is {0}.", balance);
        }

        public void Print()
        {
            Console.WriteLine("Current balance of {0} is {1}.", name, balance);
        }

        // no need for destructor, its done automaticaly in garbage collector
    }
}



Any comments or suggestions are welcome. Thank you.

解决方案

Since a Deposit is logically impossible without an Account to deposit it in, you need to have an instance of the BankAccount class ready to receive the money. So the way you have it is already correct:

myBank.Deposit(money);


The confusion comes when you try to deposit money via a user, not because you "have to create an object of that class" but because you haven't established a link between the User and the Account.

Think about it: when you (User) goto the bank with a fist full of your hard-earned money, the first thing they want to know is "What account?" - if only because (like me and many others) you may have more than one account with the same bank: Checking and Deposit accounts for example.

So it isn't that you want to create and instance of the account, but rather that you need to find out the instance of the account that the user is planning to deposit into: and he may be depositing the money into a number of accounts at the same time - 50% here, 20% there and 30% other there please!

The next fun is that should "Michal" be allowed to deposit or withdraw money on that account? :laugh:

So think about how you need to operate in your Main method - work out what happens with a real bank, and try to emulate that.


这篇关于从另一个类访问方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆