求Java大神编写程序

2024-12-13 06:45:17
推荐回答(1个)
回答1:

public class Main {
public static void main(String[] args) {
//输出货物总重
System.out.println(Obj.nSum);

//增加货物
Obj A = new Obj(1);
Obj B = new Obj(2);

System.out.println(Obj.nSum);

Obj C = new Obj(3);
Obj D = new Obj(4);

System.out.println(Obj.nSum);
System.out.println(A.getM_a() + " + " + B.getM_a() + " + " + C.getM_a() + " + " + D.getM_a() + " = " + Obj.nSum);
}
}

class Obj {
public static int nSum;
private int m_a;

public Obj(int m_a) {
this.m_a = m_a;
nSum += m_a;
}

public int getM_a() {
return m_a;
}

}