访问器未打印出正确的输出
Accessors not printing out the correct output
我试图让我的程序打印下面程序中定义的 4 个变量(wholeNumber、decimalPart、positive、currencySymbol)中的每一个,使用每个变量的访问器,但是,当我转到 运行 程序而不是获取此
预期输出:
1
2
true
$
我反而明白了。有人可以解释导致此问题的错误吗?
CURRENT OUTPUT
代码
public class Q4 {
int wholeNumber;
int decimalPart;
boolean positive;
char currencySymbol;
public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){
}
public static void main(String[] args) {
Q4 m = new Q4(1,2,true,'$');
System.out.println(m.getWholeNumber());
System.out.println(m.getDecimalPart());
System.out.println(m.isPositive());
System.out.println(m.getCurrencySymbol());
}
// Accessor
public int getWholeNumber(){
return(wholeNumber);
}
// Accessor
public int getDecimalPart(){
return(decimalPart);
}
// Accessor
public boolean isPositive(){
return(positive);
}
// Accessor
public char getCurrencySymbol(){
return(currencySymbol);
}
}
您忘记在构造函数中设置成员变量,所以它们保持默认值。
public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){
this.wholeNumber = wholeNumber;
this.decimalPart = decimalPart;
this.positive = positive;
this.currencySymbol = currencySymbol;
}
我试图让我的程序打印下面程序中定义的 4 个变量(wholeNumber、decimalPart、positive、currencySymbol)中的每一个,使用每个变量的访问器,但是,当我转到 运行 程序而不是获取此
预期输出:
1
2
true
$
我反而明白了。有人可以解释导致此问题的错误吗?
CURRENT OUTPUT
代码
public class Q4 {
int wholeNumber;
int decimalPart;
boolean positive;
char currencySymbol;
public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){
}
public static void main(String[] args) {
Q4 m = new Q4(1,2,true,'$');
System.out.println(m.getWholeNumber());
System.out.println(m.getDecimalPart());
System.out.println(m.isPositive());
System.out.println(m.getCurrencySymbol());
}
// Accessor
public int getWholeNumber(){
return(wholeNumber);
}
// Accessor
public int getDecimalPart(){
return(decimalPart);
}
// Accessor
public boolean isPositive(){
return(positive);
}
// Accessor
public char getCurrencySymbol(){
return(currencySymbol);
}
}
您忘记在构造函数中设置成员变量,所以它们保持默认值。
public Q4(int wholeNumber, int decimalPart, boolean positive, char currencySymbol){
this.wholeNumber = wholeNumber;
this.decimalPart = decimalPart;
this.positive = positive;
this.currencySymbol = currencySymbol;
}