У меня есть суперкласс под названием 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!");
}
}
Код: Выделить всё
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();
}
}
Код: Выделить всё
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);
}
Спасибо.>
Подробнее здесь: https://stackoverflow.com/questions/645 ... iable-java
Мобильная версия