Advertisement
arthur393

Herança

Apr 14th, 2014
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class Fatec {
  4.  
  5.         private ArrayList<Pessoa> pessoas;
  6.        
  7.         public Fatec(){
  8.             pessoas = new ArrayList<Pessoa>();
  9.         }
  10.        
  11.         public static void main(String[] args){
  12.             Fatec f = new Fatec();
  13.            
  14.            
  15.            
  16.         }
  17.        
  18.         public void cadastra(Pessoa p){
  19.             pessoas.add(p);
  20.            
  21.         }
  22.         public void listaPessoa(){
  23.             for( Pessoa p: pessoas){
  24.                 System.out.println(p.getNome() + " " + p.getPapel() + " ");
  25.                 if(p.getClass().toString().equals("class Aluno")){ //getClass revela a qual  classe pertencente a herança
  26.                     //if (p instanceof Aluno){  
  27.                     //  ...
  28.                     //}
  29.                    
  30.                     Aluno temp = (Aluno)p; //atribuindo p convertido à origem( Aluno )
  31.                     System.out.println(temp.getMatricula());
  32.                 }
  33.                 else{
  34.                     System.out.println();
  35.                    
  36.                 }
  37.             }
  38.         }
  39.  
  40. }
  41.  
  42. public abstract class Pessoa {
  43.     private String nome;
  44.     private String email;
  45.     private String cpf;
  46.        
  47.     public String getCpf() {
  48.         return cpf;
  49.     }
  50.  
  51.     public void setCpf(String cpf) {
  52.         this.cpf = cpf;
  53.     }
  54.  
  55.     public Pessoa(String n){
  56.         nome = n;
  57.     }
  58.  
  59.     public String getNome() {
  60.         return nome;
  61.     }
  62.  
  63.  
  64.     public void setNome(String nome) {
  65.         this.nome = nome;
  66.     }
  67.  
  68.  
  69.     public String getEmail() {
  70.         return email;
  71.     }
  72.  
  73.  
  74.     public void setEmail(String email) {
  75.         this.email = email;
  76.     }
  77.    
  78.     public abstract String getPapel();
  79.  
  80.  
  81. }
  82.  
  83.  
  84. public class Aluno extends Pessoa {
  85.     private int matricula;
  86.     public Aluno(String n, int m ){
  87.         super(n);
  88.         matricula = m;
  89.     }
  90.  
  91.  
  92.     public int getMatricula() {
  93.         return matricula;
  94.     }
  95.  
  96.  
  97.     public void setMatricula(int matricula) {
  98.         this.matricula = matricula;
  99.     }
  100.  
  101.  
  102.     public String getPapel() {
  103.  
  104.         return "Aluno";
  105.     }
  106.  
  107.  
  108. }
  109.  
  110.  
  111.  
  112. public class Professor extends Pessoa {
  113.  
  114.     public Professor(String n){
  115.         super(n);
  116.     }
  117.     public String getPapel() {
  118.  
  119.         return "Professor";
  120.     }
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement