Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何收集所有ο;从arraylist中的抽象类创建的对象_Java_Abstract Class - Fatal编程技术网

Java 如何收集所有ο;从arraylist中的抽象类创建的对象

Java 如何收集所有ο;从arraylist中的抽象类创建的对象,java,abstract-class,Java,Abstract Class,如何在arraylist中收集从抽象类创建的所有ο对象 以下是我到目前为止所做的尝试: package exercice3; import java.util.ArrayList; import java.util.List; public abstract class Personne { protected String nom ; protected String prenom ; protected String adresse ; pro

如何在arraylist中收集从抽象类创建的所有ο对象

以下是我到目前为止所做的尝试:

package exercice3;

import java.util.ArrayList;
import java.util.List;

public abstract class Personne {
    
    protected String nom ;
    protected String prenom ; 
    protected String adresse ;
    protected static int nbPersonnes = 0 ; 
    List<Personne> ListePersonnes = new ArrayList <>();
    
    public Personne(String nom , String prenom , String adresse) {
        
        this.nom= nom ; 
        this.prenom = prenom ; 
        this.adresse = adresse ; 
        nbPersonnes++;
        ListePersonnes.add(nbPersonnes, );
    }
}
package exercie3;
导入java.util.ArrayList;
导入java.util.List;
公共抽象类人员{
保护字符串名称;
保护字符串prenom;
保护字符串地址;
受保护的静态内部人员=0;
List ListPersonnes=new ArrayList();
公共人员(字符串名称、字符串前缀、字符串地址){
这个.nom=nom;
this.prenom=prenom;
this.adrese=adrese;
nbPersonnes++;
添加(nbPersonnes,);
}
}
公共抽象类人员{
保护字符串名称;
保护字符串prenom;
保护字符串地址;
受保护的静态内部人员=0;
静态列表ListPersonnes=new ArrayList();
公共人员(字符串名称、字符串前缀、字符串地址){
这个.nom=nom;
this.prenom=prenom;
this.adrese=adrese;
nbPersonnes++;

ListPersonnes.add(此);//每次创建对象时,都会为该对象创建一个新的ListPersonne。您需要将列表设置为静态,并使用类似这样的类名调用/获取它,Personne。ListPersonnes;我自己怀疑您的代码是否正在编译。您正试图将
int
添加到类型
Personne
的集合中,您不能ot实例化Personne类,因为它是抽象的。为什么可以只在ListPersonne中放入扩展Personne的非抽象类对象?为什么ListPersonne在这种情况下应该是静态的?最好将列表初始化为
Collections.synchronizedList(new ArrayList())
以确保线程安全,因为列表是静态的。
public abstract class Personne {
           
    protected String nom ;
    protected String prenom ; 
    protected String adresse ;
    protected static int nbPersonnes = 0 ; 
    static List<Personne> ListePersonnes = new ArrayList<>();
    
    
    
    public Personne(String nom , String prenom , String adresse) {
        
        this.nom= nom ; 
        this.prenom = prenom ; 
        this.adresse = adresse ; 
        nbPersonnes++;
        ListePersonnes.add(this);  // <== use 'this' keyword
    
    }
 }