打印来自母 class 对象的扩展 class 对象的值

Print a value of an extended class object from mother class object

我已经声明了一个 table 具有母 class 类型并且我已经用许多具有扩展 classe 类型的对象填充它,一切看起来都很好并且table 已成功填充,问题是当我尝试访问我无法访问的 table 值时(在示例中我试图获取 salaire属性)
母亲 CLASS

package TP4_exo2;

     public class personne {
        private String nom,prenom,date_de_naissance;
    
        
        public personne(){}
        public personne(String nom, String prenom, String date_de_naissance) {
            this.nom = nom;
            this.prenom = prenom;
            this.date_de_naissance = date_de_naissance;
        }
    
        
        public void affiche() {
            System.out.println("\nnom :"+this.nom+
                    "\nprenom :"+this.prenom+
                    "\ndate de naissance :"+this.date_de_naissance);
        }
    }

SUB CLASS 1

package TP4_exo2;

public class employé extends personne {
   
   Double salaire;

   public employé() {
       super();
       salaire=0.0;
   }

   public employé(String nom, String prenom, String date_de_naissance,Double salaire) {
       super(nom,prenom,date_de_naissance);
       this.salaire=salaire;
   }


   public void affiche() {
       super.affiche();
       System.out.print("salaire :"+this.salaire);
   }

}


主要class


    package TP4_exo2;
    
    import java.util.Scanner;
    
    public class programme4 {
    
    
        public static void main(String[] args) {
            personne tab [] =  new personne[2];
            Scanner sc = new Scanner(System.in);
            int i,j;
            
            for(i=0;i<tab.length;i++) {
                
                personne emp1;//declaration
                
                System.out.println("Donner le nom "+i);//demande informations
                String nom = sc.next();
                System.out.println("Donner le prenom "+i);
                String prenom = sc.next();  
                System.out.println("Donner la date de naissance "+i);
                String date = sc.next();
                System.out.println("Donner le salaire "+i);
                Double salaire = sc.nextDouble();
                
                emp1 =  new employé(nom,prenom,date,salaire);//instanier l'obj avec ls info
                tab[i] = emp1;//affecter au tableau
    
            }
            
        
            
            for(i=0;i<tab.length;i++) {
                System.out.println("EMPLOYER "+i+"\n");
    
                if(tab[i].salaire>45000)**//Exception in thread "main" 
               //java.lang.Error: Unresolved compilation problem: 
               //salaire cannot be resolved or is not a field**
                {
                    tab[i].affiche();
                }
            }
            
            
        }
    
    }

您已告诉编译器 tab 包含 personne 个对象。编译器只知道你告诉它什么。

编译器不执行您的程序。它不知道您的程序可能已将 employé 实例添加到数组中。就此而言,它不会在程序中的每个方法调用之后检查是否有任何这些方法可能添加了不同的对象。

由于编译器只能确定 tab 包含类型 personne 或其子类型的对象,因此您必须自己检查每个元素的类型:

if (tab[i] instanceof employé emp && emp.salaire > 45000)
{
    tab[i].affiche();
}

如果您使用的 Java 版本早于 Java 14,则必须手动执行转换:

if (tab[i] instanceof employé)
{
    employé emp = (employé) tab[i];
    if (emp.salaire > 45000)
    {
        tab[i].affiche();
    }
}