Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 对象作为参数_Java - Fatal编程技术网

Java 对象作为参数

Java 对象作为参数,java,Java,嗨,我是java程序员的新手,也是我们大学的一年级新生。我目前正在练习我的java程序,遇到了一个问题,我不知道如何编写代码,在代码中我有一个对象作为参数,并希望有一个值 public int transferTo(Account another, int amount) { if (amount<=balance) // I dont know how to use the object another to have the value of amount put in

嗨,我是java程序员的新手,也是我们大学的一年级新生。我目前正在练习我的java程序,遇到了一个问题,我不知道如何编写代码,在代码中我有一个对象作为参数,并希望有一个值

public int transferTo(Account another, int amount)
{
   if (amount<=balance)
     // I dont know how to use the object another to have the value of amount put into the object.
     // another = amount; it causes a compiler error

}
public int transferTo(另一账户,int金额)
{

如果(amount像使用本地对象一样使用参数对象。在使用它之前,应该检查参数是否为null

public int transferTo(Account another, int amount) {
   if (another == null) {
       return 0;
   }

   if (another.hasEnough(amount)) {
      another.subtractFromBalance(amount);
   }
   ....



}

您可以像使用本地对象一样使用parameter对象。在使用它之前,应该检查参数是否为null

public int transferTo(Account another, int amount) {
   if (another == null) {
       return 0;
   }

   if (another.hasEnough(amount)) {
      another.subtractFromBalance(amount);
   }
   ....



}
您的
transferTo(Account-other,int-amount)
方法将执行类似的操作

您可以创建一个名为
InsufficientFundsException的类

class InsufficientFundsException extends Exception {
   public InsufficientFundsException(String msg){
        super(msg);
   }
}

您的
transferTo(Account-other,int-amount)
方法将执行类似的操作

您可以创建一个名为
InsufficientFundsException的类

class InsufficientFundsException extends Exception {
   public InsufficientFundsException(String msg){
        super(msg);
   }
}



粘贴
Account
class here对象具有根据其可见性而可用的字段和方法。您可以使用另一个.balance或另一个.getBalance()您需要在
帐户
类中有一个实例方法来设置其金额。请将
金额.class
粘贴到此处。您还想做什么?将金额从
此.class
转移到
另一个对象
?@SiddarthSreeni是的,先生,我正试图将金额的值转移到另一个对象粘贴
Account
class here对象的字段和方法根据其可见性而可用。您可以使用另一个.balance或另一个.getBalance()您需要在
帐户
类中有一个实例方法来设置它的金额。请将
金额.class
粘贴到此处。您还想做什么?将金额从
此.class
转移到
对象另一个对象
?@SiddarthSreeni是的,先生,我正试图将金额的值转移到另一个对象。您怎么知道这些公共方法?我创建它们是为了解释。@NomadMaker amount的值将如何添加到另一个对象?您将创建这些方法。这是非常基本的Java编程。我会编辑我的答案来解释它,但有人已经否决了我的答案。如果(另一个==null)返回0;它只会导致编译器错误sir“二进制运算符'=''的操作数类型错误”你是怎么知道这些公共方法的?我创建它们是为了解释。@NomadMaker amount的值将如何添加到另一个对象中?你应该创建这些方法。这是非常基本的Java编程。我会编辑我的答案来解释它,但有人已经否决了我的答案。if(other==null)返回0;它只会导致编译器错误sir“二进制运算符'=''的操作数类型错误”