0
I’m doing some exercises and I came across a problem, follow part of my code:
class Empresa{
public String nome;
public String cnpj;
private Funcionario[] empregados;
public String rua;
public String bairro;
public int livre = 0;
public Funcionario getFuncionario(int posicao){
return this.empregados[posicao];
}
public Funcionario getEmpregados(){
return this.empregados = empregados;
}
//resto da classe
//....
//Segunda classe
//
class EmpresaTeste{
public static void main(String[] args){
Empresa empresa = new Empresa();
empresa.empregados = new Funcionario[10];
Funcionario f1 = new Funcionario();
f1.nome = "Primeiro funcionario";
f1.departamento = "Faxina";
f1.setSalario(1000);
f1.setRg("37263857634");
f1.data = new Data();
f1.data.preencheData(12, 11, 2011);
empresa.adiciona(f1);
//...
if(empresa.contem(empresa.getFuncionario(2))){
System.out.println("");
System.out.println("");
System.out.println("Ok");
}else{
System.out.println("");
System.out.println("");
System.out.println("Funcionario não encontrado");
}
}}
Yeah, on the fourth line of EmpresaTeste
I’m starting the array employees of the class Empresa
, but the attribute is as private
, at the end of the class EmpresaTeste
have a if
that I was able to gain access to the employee attribute through the get
. How do I gain access when I have to start a array?
If I do
empresa.getEmpregados = new Funcionario[10];
he will return me a mistake.
Each class must handle its own initialization. If you want to create a company with 10 employees, the idiomatic way to do it is through the constructor:
new Empresa(10)
, ornew Empresa(new Funcionario[10])
.– dcastro
Yeah, but like I said, I’m following the exercises.
– Naldson
You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Something needs to be improved?
– Maniero