如何更新实例变量的值 JAVA

how to update value of instance variable JAVA

我是 JAVA 和 OOP 的新手,我正在尝试做一些 OOP 练习,但坚持更新 supper class.

的实例变量的值

我有一个 super-class 叫做 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!");
    }
}

和 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();  
    }
}

和主要功能

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);
        
    }

我的问题是如何在减法和加法后更新supper中实例变量paper的值class?

谢谢。

首先,你不应该在子class中有一个_color_paper实例变量,因为父class Print 已经定义了这些变量。

第二,看这两行

temp = super.getPaper();   
temp -= paper;

你是减去局部变量temp的值,它与superclass中的_print变量无关。您应该调用 super.setPaper(temp) 来设置 _print 的值。

parentclass没问题,但是child和main函数有些问题

  1. 将子 class 变量声明为与父变量相似是一种不好的做法,因为它会造成混淆。但是,它仍然是合法的,因为父变量对子变量是隐藏的 class。 所以,最好去掉:
private String _color; //not used in child class
private int _paper; //not used in child class
  1. runPrint方法
@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());
    }
}
  1. 并且由于您正在处理 Print 的数组,循环很方便。
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);
    }
    
}