对象上的设置器(Setters)Java

huangapple go评论78阅读模式
英文:

Setters on objects Java

问题

你好,我在处理对象的设置方法方面遇到了一些困难。

以下是一个示例代码:

public class Company {
    private String companyName;
    public static int numberOfEmployees;
    public Employees employees[];

    public void setEmployees(String name, String heritage, String[] programmingLanguages, Salary d) {
        Employees employee1 = new Programmer(name, heritage, programmingLanguages, d, d.getBasicBrutoSalary());
        employees[numberOfEmployees] = employee1;
        numberOfEmployees++;
    }
}

基本上,这是在“公司类”中定义的一个方法,用于创建一个使用参数构建“程序员”的“员工对象”。

但问题不在于此,我想做的是通过调用这个设置方法,自动创建一个对象。因此,每次使用它时,都会递增要创建的对象的名称。

例如,第一次使用它时,它会创建一个名为Employee1的对象,并将其存储在Employee[0]中... 第二次使用时,我希望将Employee2存储到Employee[1]中。

也许我把这事情想得太复杂了,但我只是在尝试一些方法,似乎找不到让这个工作的方法。

英文:

Hello so I'm having a bit of difficulties with a setter method on objects.

public class Company {
    private  String companyName;
    public static int numberOfEmployees;
    public Employees employees[];

    public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
      
        Employees employee1  = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());
        employees[numberOfEmployees] = employee1;
        numberOfEmployees++;

    }

So basicly this is a method defined in the 'company class' while making an Employees object who's using the parameters for making a 'Programmer'.

But that's not the deal, what I want tot do is by calling this setter method, automaticly create an object. So each time it's used, kind of increment the name of the object it's going to make.

So for example the first time I use it it makes an object called Employee1 and stores it in Employee[0].. second time I want it to store Employee2 into Employee[1].

Maybe I'm making this way too difficult but I'm just trying things out, and can't seem to find a way to make this work.

答案1

得分: 0

我假设 Programmer 对象是 Employees 的子类,否则它将无法工作。或多或少地,它应该类似于以下内容:

public class Company {
    private String companyName;
    public static int numberOfEmployees;
    public static Employees employees[];

    public void setEmployees(String name, String heritage, String [] programmingLanguages, Salary d) {
        numberOfEmployees++;
        employees[numberOfEmployees] = new Programmer(name, heritage, programmingLanguages, d, d.getBasicBrutoSalary());
    }
}
英文:

I suppose that Programmer object is subclass of Employees, or else it will not work. More or less it should look like the following:

public class Company {
private  String companyName;
public static int numberOfEmployees;
public static Employees employees[];

public void setEmployees( String name, String heritage, String [] programmingLanguages, Salary d) {
	numberOfEmployees++;
	employees[numberOfEmployees] = new Programmer(name, heritage,programmingLanguages, d,d.getBasicBrutoSalary());;
}

huangapple
  • 本文由 发表于 2020年10月21日 23:26:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64466846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定