我在下面创建了类,并在Java项目中为2名员工启动了代码。 我应该如何修改类(强制使用私有属性和公共方法)和代码,以便在应实现数组的项目中使用此类。 想法是例如启动10次循环,并为所有分配的值打印输出。
班级
"extra": {
...
"installer-paths": {
"web/libraries/colorbox": ["npm-asset/jquery-colorbox"],
"web/libraries": [
"type:drupal-library",
"type:component",
"type:bower-asset",
"type:npm-asset"
],
}
}
}
代码
package radomirbz4;
public class RBZ4C {
private String Employeename ;
private String JMBG;
private Double Salary;
private int Experience;
void newName(String newName) {
Employeename = newName;
}
void newJMBG(String newJMBG) {
JMBG = newJMBG;
}
void newSalary(double newSalary) {
Salary = newSalary;
}
void newExperience(int newExperience) {
Experience = newExperience;
}
void printChanges() {
System.out.println("Employee: "+Employeename+"with JMBG: "+JMBG+"has salary"+Salary+"due experience of"+Experience+"years.");
}
}
答案 0 :(得分:0)
您可能希望将RBZ4C对象放置在RadomirBZ4类的数组中,如下所示:
List<RBZ4C> employees = new ArrayList<>();
for (int i = 0; i < 10) {
employees.add(new RBZ4C());
}
在此之后,您可以在类似这样的元素上运行方法:
employees.get(0).newName("Radomir Brzakovic");
如果您要打印所有员工的日期,可以这样:
for (int i = 0; i < employees.length; i++) {
System.out.println(employees.get(i).printChanges());
}