英文:
how to update value of instance variable JAVA
问题
我是JAVA和面向对象编程的新手,我正在尝试做一些面向对象编程的练习,但在更新超类(父类)的实例变量值时遇到了问题。
我有一个名为Print的超类:
```java
public class Print {
private String _color;
private int _paper;
public Print(int paper, String color) {
this._color = color;
this._paper = paper;
}
// getter
public String getColor() {
return this._color;
}
public int getPaper() {
return this._paper;
}
// setter
public void setColor(String color) {
this._color = color;
}
public void setPaper(int paper) {
this._paper = paper;
System.out.println("当前纸张数量:" + this._paper);
}
// runPrint
public void runPrint(int paper) {
System.out.println("这是演示!");
return;
}
// addPaper
public void addPaper(int paper) {
System.out.println("这是演示!");
}
}
还有一个子类ColorPrint:
public class ColorPrint extends Print {
private String _color;
private int _paper;
public ColorPrint(int paper, String color) {
super(paper, color);
}
// runPrint
@Override
public void runPrint(int paper) {
int temp = 0;
if(super.getPaper() - paper < 0) {
paper -= super.getPaper();
System.out.println(super.getColor() + "纸张需要" + paper + "张!");
} else {
System.out.println(super.getColor() + " " + super.getPaper() + "张已打印。");
temp = super.getPaper();
temp -= paper;
System.out.println(super.getColor() + "剩余" + temp + "张。");
}
return;
}
// addPaper
@Override
public void addPaper(int paper) {
System.out.println(paper + "张已添加。");
int currPaper = super.getPaper() + paper;
super.setPaper(currPaper);
}
@Override
public String toString() {
return super.getColor() + ": " + "当前纸张为 " + super.getPaper();
}
}
还有主函数部分:
public static void main(String[] args) {
Print[] p = {
new ColorPrint(100, "彩色")
};
// 打印出可用的100张纸
// 打印后当前纸张为零。
p[0].runPrint(100);
// 然后我添加了50张纸
// 现在当前纸张应为50张。
// 但是它输出了150。我在这个地方卡住了。
p[0].addPaper(50);
}
我的问题是,如何在减去和添加纸张后更新超类(父类)中的实例变量“_paper”的值?
谢谢。
<details>
<summary>英文:</summary>
I'm new to JAVA and OOP, I'm trying to do some OOP exercises but stuck on updating the value of the instance variable of supper class.
I have a super-class called Print
public class Print {
private String _color;
private int _paper;
public Print(int paper, String color) {
this._color = color;
this._paper = paper;
}
// getter
public String getColor() {
return this._color;
}
public int getPaper() {
return this._paper;
}
// getter
public void setColor(String color) {
this._color = color;
}
public void setPaper(int paper) {
this._paper = paper;
System.out.println("current amount of paper: " + this._paper);
}
// runPrint
public void runPrint(int paper) {
System.out.println("this is demo!");
return;
}
// addPaper
public void addPaper(int paper) {
System.out.println("this is demo!");
}
}
and a child class ColorPrint
public class ColorPrint extends Print {
private String _color;
private int _paper;
public ColorPrint(int paper, String color) {
super(paper, color);
}
// runPrint
@Override
public void runPrint(int paper) {
int temp = 0;
if(super.getPaper() - paper < 0) {
paper -= super.getPaper();
System.out.println(super.getColor() + " paper needs " + paper + " more!");
} else {
System.out.println(super.getColor() + " " + super.getPaper() + " is printed.");
temp = super.getPaper();
temp -= paper;
System.out.println(super.getColor() + " is remains for " + temp);
}
return;
}
// addPaper
@Override
public void addPaper(int paper) {
System.out.println(paper + " is added.");
int currPaper = super.getPaper() + paper;
super.setPaper(currPaper);
}
@Override
public String toString() {
return super.getColor() + ": " + "current paper is " + super.getPaper();
}
}
and the main function
public static void main(String[] args) {
Print[] p = {
new ColorPrint(100, "Color")
};
// print out the available 100 papers
// after printed the current paper now is zero.
p[1].runPrint(100);
// then I add 50 papers
// the current paper now must be 50.
// But it prints out 150. I'm stuck on this.
p[1].addPaper(50);
}
My question is how can I update the value of instance variable paper in the supper class after subtracted and added?
Thank you.
</details>
# 答案1
**得分**: 0
**首先**,在子类中不应该有 `_color` 和 `_paper` 实例变量,因为父类 `Print` 已经定义了这些变量。
**其次**,看一下这两行代码
temp = super.getPaper();
temp -= paper;
你在减去一个局部变量 `temp` 的值,它与父类中的 `_print` 变量无关。你应该调用 `super.setPaper(temp)` 来设置 `_print` 的值。
<details>
<summary>英文:</summary>
**First**,you should not have a `_color` and `_paper` instance variable in the subclass because parent class `Print` has already difined these variable.
**Second**, look at this two line
temp = super.getPaper();
temp -= paper;
You are substract the value of a local variable `temp`, it has nothing to do with the `_print` variable in super class. You shoud call `super.setPaper(temp)` to set the value of `_print`.
</details>
# 答案2
**得分**: 0
父类没问题,但子类和主函数存在一些问题。
1. 声明子类变量与父类相似是一个不好的做法,会造成混淆。但这仍然是合法的,因为父类变量对子类是隐藏的。所以最好移除:
```java
private String _color; //在子类中未使用
private int _paper; //在子类中未使用
runPrint
方法
@Override
public void runPrint(int paper) {
if (paper > super.getPaper()) {
int requiredPapers = paper - super.getPaper();
System.out.println(super.getColor() + " 纸张需要 " + requiredPapers + " 张!");
} else {
System.out.println(super.getColor() + " " + super.getPaper() + " 张已打印。");
super.setPaper(super.getPaper() - paper); // 在此处使用 setter/mutator 方法
System.out.println(super.getColor() + " 剩余 " + super.getPaper() + " 张纸张");
}
}
- 由于你正在处理
Print
数组,使用循环会更方便。
public static void main(String[] args) {
Print[] p = { new ColorPrint(100, "彩色") };
for (int i = 0; i < p.length; i++) {
// 打印出可用的 100 张纸张
// 打印当前纸张后,剩余数量为零。
p[i].runPrint(100);
// 然后我添加了 50 张纸张
// 当前纸张数量应为 50。
// 但是实际打印出了 150 张。我在这个地方卡住了。
p[i].addPaper(50);
}
}
英文:
The parent class is ok, but there are some issues with the child and the main function.
- It is a bad practice to declare child class variables similar to the parent, since it creates confusion. But, it is still legal since parent variables are hidden from child class.
So, it is better to remove:
private String _color; //not used in child class
private int _paper; //not used in child class
- The
runPrint
method
@override
public void runPrint(int paper) {
if(paper > super.getPaper()) {
int requiredPapers = paper - super.getPaper());
System.out.println(super.getColor() + " paper needs " + requiredPapers + " more!");
} else {
System.out.println(super.getColor() + " " + super.getPaper() + " is printed.");
super.setPaper(super.getPaper() - paper); // HERE use setter/mutator method
System.out.println(super.getColor() + " is remains for " + super.getPaper());
}
}
- And since you are dealing with an array of
Print
, loops are convinient.
public static void main(String[] args) {
Print[] p = { new ColorPrint(100, "Color") };
for(int i = 0; i < p.length; i++) {
// print out the available 100 papers
// after printed the current paper now is zero.
p[i].runPrint(100);
// then I add 50 papers
// the current paper now must be 50.
// But it prints out 150. I'm stuck on this.
p[i].addPaper(50);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论