繁体   English   中英

如何将继承的变量分配给子类的 arrayList object

[英]How to assign the inherited variables to the arrayList object of a subclass

我有一个abstract class Element:

public abstract class Element{
    String nom;
    int prix;

    // constructor
    public Element(String n, int p) {
        this.nom = n;
        this.prix = p;
    }

    //getters
    public String getNom() {
        return nom;
    }

    public int getPrix() {
        return prix;
    }
}

我有一个从class Element扩展而来的subclass Bag ,其特点是重量( poids )和颜色( couleur )。 而且Bag还包含一个对象列表(对于 Object 类)。

所以我想做的是在我的subclass Bag中创建一个arrayList<superclass> ,以便能够编写代码来表示这些对象:一个 Apple(一个苹果的价格:7 rubies),一个 Banana(价格 = 5红宝石)和一条鱼(价格 = 20 红宝石),也可以在袋子中添加或移除物品。

我的问题是:如何将继承的变量分配给我的“Bag”子类的 object arraylist objectsInBag

我正在使用这个问题中的代码,如果我正在尝试的话,应该如何创建子类的构造函数,包括arrayList objectsInBag

我到目前为止的代码:

import java.util.ArrayList;

public class Bag extends Element{

// atributtes propres a Bag
private String couleur;
private Integer poids;

ArrayList<Object[]> objectsInBag = new ArrayList<>();


// constructor

public Bag(String n, Integer p, String couleur, Integer poids ) {
    
    super(n, p);
    this.couleur = couleur;
    this.poids = poids;
}

// adding object into the bag
public void addObjects(String n, Integer p){
    objectsInBag.add(new Object[]{getNom(), getPrix()});
}

// getters and setters

public String getCouleur(){
    return this.couleur;
}

public void setCouleur(String couleur){
    this.couleur = couleur;
}

public Integer getPoids(){
    return this.poids;
}

public void setPoids(Integer poids){
    this.poids = poids;
}

据我了解,您正在尝试做两件事:

  1. 超类Objet (误导很多,最好重命名为Thing或抽象的东西,与标准 java Object )及其继任者:Apple、Banana、Fish
  2. Bag是一个容器,可以存放一些东西。

为此,您必须有一个层次结构:

abstract class Thing{
  private final String nom;
  private final int prix;
  //+ constructor, getters
}

class Apple extends Thing {
 //+constructor that just calls `super(nom, prix)`
}

class Banana extends Thing {
 //+constructor that just calls `super(nom, prix)`
}

class Fishextends Thing {
 //+constructor that just calls `super(nom, prix)`
}

容器Bag将被生成为Thing类型,如下所示:

class Bag<T> {
  // atributtes propres a Bag
  private final String couleur;
  private final Integer poids;
  //+ constructor, getters
  private final List<T> list = new ArrayList<>();

  addItem(T item){
    list.add(item);
  }

  removeItem(T item){
    list.remove(item);
  }
}

你可以这样使用它:

Bag<Thing> bag = new Bag<>("couleur", 5);
bag.addItem(new Apple("rubies", 7));
bag.addItem(new Banana ("rubies", 5));
bag.addItem(new Fish("rubies", 20));

您似乎需要修改 class Bag以获得BagItem实例列表而不是List<Object[]> BagItem应该是AppleBananaFish等的超类,并且可能具有将包项与特定包关联的字段。

public class Bag extends Objet {

    // atributtes propres a Bag
    private String couleur;
    private Integer poids;

    List<BagItem> objectsInBag = new ArrayList<>();

    public Bag(String n, Integer p, String couleur, Integer poids ) {
        super(n, p);
        this.couleur = couleur;
        this.poids = poids;
    }

    // adding object into the bag
    public boolean addItem(BagItem item) {
        item.setBag(this);
        return objectsInBag.add(item);
    }

    // remove object from bag
    public boolean removeItem(BagItem item) {
        Objects.requireNonNull(item);
        boolean res = objectsInBag.remove(item);
        if (res) {
            item.setBag(null); // clear reference to bag if removal succeeded
        }
        return res;
    }
}
public class BagItem extends Objet {
    private Bag bag;

    public BagItem(String n, Integer p) {
        this(n, p, null); // unknown bag
    }

    public BagItem(String n, Integer p, Bag bag) {
        super(n, p);
        if (null != this.bag) {
            this.bag.addItem(this);
        }
    }

    public void setBag(Bag bag) {
        this.bag = bag;
    }
}

扩展 BagItem 的示例BagItem —— 只有构造函数可能需要重新定义:

public class Apple extends BagItem {
    public Apple(String n, Integer p) {
        this(n, p, null);
    }

    public Apple(String n, Integer p, Bag bag) {
        super(n, p, bag);
    }
}

用法:

  • 创建一个Bag实例
  • 创建具体的BagItem实例(可选地使用Bag实例)
Bag redBag = new Bag("myBag", 5, "Red", 10);
// added immediately
Apple apple1 = new Apple("Golden", 7, redBag);

// added via .addItem
Apple apple2 = new Apple("Jonagold", 8);
redBag.addItem(apple2);

System.out.println("remove golden apple: " + redBag.removeItem(apple1));

如果我理解正确,您只想将数组的初始化包含在子类的构造函数中。 您可以像这样更改代码:

public class Bag extends Element{

    // atributtes propres a Bag
    private String couleur;
    private Integer poids;

    // constructor
    public Bag(String n, Integer p, String couleur, Integer poids ) {
        super(n, p);
        this.couleur = couleur;
        this.poids = poids;
        ArrayList<Object[]> objectsInBag = new ArrayList<>();
    }

    // adding object into the bag
    public void addObjects(String n, Integer p){
        objectsInBag.add(new Object[]{n, p});
    }

    // getters and setters
    public String getCouleur(){
        return this.couleur;
    }

    public void setCouleur(String couleur){
       this.couleur = couleur;
    }

    public Integer getPoids(){
        return this.poids;
    }

    public void setPoids(Integer poids){
       this.poids = poids;
    }
}

我使用@MikhailKopylov 的答案并应用多态性原理解决了它:

所以, abstract class Element

public abstract class Element{
    String nom;
    int prix;

    // constructor
    public Objet(String n, int p) {
        this.nom = n;
        this.prix = p;
    }

    //getters
    public String getNom() {
        return nom;
    }

    public int getPrix() {
        return prix;
    }

}

// the name and price of each object are already determined in the Apple, Banana and Fish classes

class Apple extends Element {
//+constructor that just calls `super(nom, prix)`
public Apple(String n, int p) {
    super("Pomme", 7);
}

}

class Banana extends Element {
//+constructor that just calls `super(nom, prix)`
public Banana(String n, int p) {
    super("Banane", 5);
}
}

class Fish extends Element {
//+constructor that just calls `super(nom, prix)`
public Fish(String n, int p) {
    super("Poisson", 20);
}
}

subclass Bag

public class Bag{

// atributtes propres a Bag
private String couleur;
private Integer poids;

// constructor
public Bag(String couleur, Integer poids ) {
    this.couleur = couleur;
    this.poids = poids;
}

// getters and setters
public String getCouleur(){
    return this.couleur;
}

public void setCouleur(String couleur){
   this.couleur = couleur;
}

public Integer getPoids(){
    return this.poids;
}

public void setPoids(Integer poids){
   this.poids = poids;
}

// Polymorphism

//creation of the list of object types, which allows to go on the concept of polymorphism to access the attributes of the Object class, and to assign them to an arrayList of the Bag class

List<Element> objetos = new ArrayList<Element>();

public void addItem(Element item){
    objetos.add(item);
  }

public void removeItem(Element item){
    objetos.remove(item);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM