[英]Method function isn't executed when accessed from another class
I'm studying Java, and have written the following code: 我正在学习Java,并编写了以下代码:
public class Funcionario {
String nome;
String cpf;
String rg;
String dataEntrada;
String dept;
double salario;
double ganhoAnual;
int mesesEmpresa;
public static void main(String[] args) {
Funcionario f1 = new Funcionario();
f1.nome = "Sandra";
f1.salario = 1500;
f1.mesesEmpresa = 36;
f1.recebeAumento(150);
f1.cpf = "428.768.841-10";
f1.rg = "49.757.501-6";
f1.dataEntrada = "12 dez 2006";
f1.mostra();
}
void nome() {
System.out.println("Funcionario(a): " + nome);
}
void data() {
System.out.println("Data de Entrada: " + dataEntrada);
}
void recebeAumento(double aumento) {
salario = salario + aumento;
}
double calculaGanhoAnual() {
ganhoAnual = salario * 12;
return ganhoAnual;
}
void rgEcpf() {
System.out.println("CPF: " + cpf);
System.out.println("RG: " + rg);
}
void tempoEmpresa() {
if (mesesEmpresa % 12 == 0) {
System.out.println("Tempo de casa: " + mesesEmpresa / 12 + " ano(s)");
} else {
int mesesTempo = mesesEmpresa % 12;
int anosTempo = Math.round(mesesEmpresa / 12);
System.out.println("Tempo de casa: " + anosTempo + " ano(s) e " + mesesTempo + " mes(es)");
}
}
void mostra() {
this.nome();
System.out.println("Salario atual: " + this.salario);
System.out.println("Ganho anual: " + this.calculaGanhoAnual());
this.rgEcpf();
this.tempoEmpresa();
this.data();
}
}
class Empresa {
// is this an array? then why doesn't it have the type, like int[] or long[]?
Funcionario[] empregados;
String cnpj;
String nomeEmpresa;
private int contador = 0;
public int getContador() {
return this.contador;
}
// what happens in between the parentheses?
void adiciona(Funcionario f) {
if (contador <= 10) {
this.empregados[contador] = f;
this.contador++;
} else {
System.out.println("Array cheio!");
}
}
void mostraEmpregados() {
for (int i = 0; i < this.empregados.length; i++) {
System.out.println("Funcionario na posicao: " + i);
}
}
}
class TestaEmpresa {
// why does the reference to Empresa and Funcionario array have to be inside main? Otherwise, it gives <identifier> expected error
public static void main(String[] args) {
Empresa empresa = new Empresa();
// what is happening in the next line, exactly?
empresa.empregados = new Funcionario[10];
double salarioModificado = empresa.getContador() * 1500;
Funcionario f2 = new Funcionario();
f2.salario = salarioModificado;
empresa.adiciona(f2);
Funcionario f3 = new Funcionario();
f3.salario = salarioModificado * 2;
empresa.adiciona(f3);
empresa.mostraEmpregados();
}
}
However, when I invoke empresa.mostraEmpregados();
但是,当我调用empresa.mostraEmpregados();
in the TestaEmpresa
class, void mostraEmpregados() {}
from the Empresa
class won't run. 在TestaEmpresa
类中, Empresa
类中的void mostraEmpregados() {}
将不会运行。 Therefore, the output is only: 因此,输出仅为:
Funcionario(a): Sandra
Salario atual: 1650.0
Ganho anual: 19800.0
CPF: 428.768.841-10
RG: 49.757.501-6
Tempo de casa: 3 ano(s)
Data de Entrada: 12 dez 2006
I know that we need references for classes, in order to use attributes or methods from them, but I can't get this to work. 我知道我们需要类的引用,以便使用它们中的属性或方法,但是我无法使其正常工作。 Could you guys please help me? 你们能帮我吗?
PS: please, ignore the questions in //commentary
. PS:请忽略//commentary
的问题。 I'm working on them by myself. 我一个人在研究它们。
You have two main methods: there is one on your class Funcionario
. 您有两种主要方法:类Funcionario
有一种。 Running this one produces the output that you are getting: 运行此代码将产生您得到的输出:
Funcionario(a): Sandra 函数(a):桑德拉
Salario atual: 1650.0 平均工资:1650.0
Ganho anual: 19800.0 Ganho年度:19800.0
CPF: 428.768.841-10 公积金:428.768.841-10
RG: 49.757.501-6 RG:49.757.501-6
Tempo de casa: 3 ano(s) Tempo de casa:3个
Data de Entrada: 12 dez 2006 数据日报:2006年12月12日
There is another in your class TestaEmpresa
. 您的课程TestaEmpresa
还有另一个。 Running this one produces this output: 运行此代码将产生以下输出:
Funcionario na posicao: 0 Funcionario na posicao:0
Funcionario na posicao: 1 Funcionario na posicao:1
Funcionario na posicao: 2 Funcionario na posicao:2
Funcionario na posicao: 3 Funcionario na posicao:3
Funcionario na posicao: 4 功能性排名:4
Funcionario na posicao: 5 Funcionario na posicao:5
Funcionario na posicao: 6 Funcionario na posicao:6
Funcionario na posicao: 7 功能性排名:7
Funcionario na posicao: 8 功能性排名:8
Funcionario na posicao: 9 功能性排名:9
So check your running configuration. 因此,请检查您的运行配置。 It looks that you are running the main method on Funcionario
instead of the one on TestaEmpresa
. 它看起来正在运行的主要方法Funcionario
而不是在一个TestaEmpresa
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.