Created
April 8, 2011 14:19
-
-
Save ProgDan/909926 to your computer and use it in GitHub Desktop.
Exemplo: Associação entre Classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Endereco { | |
private String logradouro; | |
private int numero; | |
private String complemento; | |
private String bairro; | |
private String cidade; | |
private String estado; | |
private int CEP; | |
public String getLogradouro() { | |
return logradouro; | |
} | |
public void setLogradouro(String logradouro) { | |
this.logradouro = logradouro; | |
} | |
public int getNumero() { | |
return numero; | |
} | |
public void setNumero(int numero) { | |
this.numero = numero; | |
} | |
public String getComplemento() { | |
return complemento; | |
} | |
public void setComplemento(String complemento) { | |
this.complemento = complemento; | |
} | |
public String getBairro() { | |
return bairro; | |
} | |
public void setBairro(String bairro) { | |
this.bairro = bairro; | |
} | |
public String getCidade() { | |
return cidade; | |
} | |
public void setCidade(String cidade) { | |
this.cidade = cidade; | |
} | |
public String getEstado() { | |
return estado; | |
} | |
public void setEstado(String estado) { | |
this.estado = estado; | |
} | |
public int getCEP() { | |
return CEP; | |
} | |
public void setCEP(int CEP) { | |
this.CEP = CEP; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pessoa { | |
private String nome; | |
private int idade; | |
private char sexo; | |
private long CPF; | |
private Endereco residencial; | |
public String getNome() { | |
return nome; | |
} | |
public void setNome(String nome) { | |
this.nome = nome; | |
} | |
public int getIdade() { | |
return idade; | |
} | |
public void setIdade(int idade) { | |
this.idade = idade; | |
} | |
public char getSexo() { | |
return sexo; | |
} | |
public void setSexo(char sexo) { | |
this.sexo = sexo; | |
} | |
public long getCPF() { | |
return CPF; | |
} | |
public void setCPF(long CPF) { | |
this.CPF = CPF; | |
} | |
public Endereco getResidencial() { | |
return residencial; | |
} | |
public void setResidencial(Endereco residencial) { | |
this.residencial = residencial; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Principal { | |
public static void main(String[] args){ | |
Pessoa vetor[]; | |
vetor = new Pessoa[5]; | |
for(int i=0; i<vetor.length;i++){ | |
vetor[i]=new Pessoa(); | |
vetor[i].setNome("Nome do fulano"); | |
vetor[i].setCPF(1234567890); | |
vetor[i].setIdade(23); | |
// Declara e Cria um endereço | |
Endereco e; | |
e = new Endereco(); | |
e.setCidade("São Paulo"); | |
e.setCEP(01023040); | |
// Atribui o endereço à pessoa | |
vetor[i].setResidencial(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Para Exibir os dados
System.out.println("Nome: "+vetor[i].getNome());
System.out.println("Cep: "+vetor[i].getResidencial().getCEP());