1、将java集合对象中的某些属性的值做运算,并将结果赋值给另外属性。(例如计算人员工资):
public class Staff{
private String name;
private int wages;//每月工资
private int month;//工作月份
private int sumWages;//总工资
//构造函数
public Staff (int wages, int month) {
this.wages = wages;
this.month = month;
}
//get和set方法
public String getName(){
return name;
}
public void setName(String name) {
this.name = name;
}
public int getWages(){
return wages;
}
public void setWages(int wages) {
this.wages = wages;
}
public int getMonth(){
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getSumWages(){
return sumWages;
}
public void setSumWages(int sumWages) {
this.sumWages = sumWages;
}
//运算方法
public void yearWages(){
this.sumWages = this.wages * this.month;
}
}
public static void main(String[] args) {
List<Staff> list = new ArrayList<>();
//创建几个Staff对象并赋值,然后将对象加到list里面,该步骤省略
//进行运算,直接调方法就可以
list.forEach(Staff::yearWages);
}