gpt4 book ai didi

java - 如何将 "get"方法返回值打印到控制台

转载 作者:行者123 更新时间:2023-12-01 13:06:53 25 4
gpt4 key购买 nike

你好,我只是在尝试一些基本的 Java 技能。

这是我的代码

package bankaccount;
public class Test {

String name;
int houseno;
int balance;

Test (String n,int h,int b) {

n=name;
h=houseno;
b=balance;

}
int getBalance(){

return balance;
}
void setBalance(int newBalance){

newBalance = balance;
}

}

//the class containing my main

package bankaccount;

/**
*
* @author ideapc
*/
public class BankAccount {
Test t1 = new Test("tim",147,5);
System.out.println(t1.getBalance() + "name =");

public static void main(String[] args) {

}

}

我正在尝试使用 system.out.println 将对象 t1 (tim) 的余额打印到控制台

我在这里做错了什么?

最佳答案

System.out.println() 放入 main() 方法中。

 public class BankAccount {
public static void main(String[] args) {
Test t1 = new Test("tim",147,5);
System.out.println("name = "+t1.getBalance());
}
}

您还需要更改 Test 的构造函数。

Test (String n,int h,int b) {
this.name=n;
this.houseno=h;
this.balance=b;
}

关于java - 如何将 "get"方法返回值打印到控制台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23194674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com