在 classes 中分配从抽象 class 扩展而来的新变量

Assigning new variables in classes that extend from an abstract class

我有一个名为 items 的抽象 class,我想创建两个名为 Appliance 和 SmallHwItem 的 classes 来扩展 items class。在我的 items abstract class 中,我有一些变量对于扩展它的 classes 都是不变的,但是我想向 appliance 和 smallhwitem classes 中的 items 构造函数添加不同的变量.例如:我想在组件 class 中包含字符串变量 "Type"、"Brand" 和 "Category"。抱歉,我是 abstract classes 的新手,非常感谢任何反馈。下面是我的两个 class。

Items.java:

package hardwarestore;

    import java.io.Serializable;

    /**
     * This class is a very simple representation of a hardware item. There are only getter
     * methods and no setter methods and as a result an item cannot be mutated once
     * initialized. An item object can also call the two override methods
     * <CODE>toString()</CODE> and <CODE>equals()</CODE>
     *
     */
    public abstract class Item implements Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private final String idNumber;
        private final String name;
        private int quantity;
        private final float price;


        public Item(String idNumber, String name, String category, int quantity, float price) {
            this.idNumber = idNumber;
            this.name = name;
            this.quantity = quantity;
            this.price = price;
        }

        /**
         * This method returns the item's tracking number.
         *
         * @return a <b><CODE>String</CODE></b> that is the ID number of the item.
         */
        public String getIdNumber() {
            return idNumber;
        }

        /**
         * This method returns the item's name.
         *
         * @return a <b><CODE>String</CODE></b> that is the item's name.
         */
        public String getName() {
            return name;
        }



        /**
         * This method returns the item's quantity.
         *
         * @return an <b><CODE>int</CODE></b> that is the item's weight
         */
        public int getQuantity() {
            return quantity;
        }

        /**
         * This method set the item's quantity.
         *
         *  @param quantity a <b><CODE>int</CODE></b> that represents the quantity
         */
        public void setQuantity(int quantity) {
            this.quantity= quantity;
        }

        /**
         * This method returns the item's price.
         *
         * @return a <b><CODE>float</CODE></b> that is the item's price
         */
        public float getPrice() {
            return price;
        }

        /**
         * This abstract method returns the item's fields as a string representation.
         *
         * @return a <b><CODE>String</CODE></b> that lists the fields of the item
         * object delineated by a space and in the same order as the constructor
         */
        public abstract String getFormattedInfo();

        /**
         * This method provides a way to compare two item objects.
         *
         * @param c a <b><CODE>Item</CODE></b> object that is used to compare to
         * <b><CODE>this</CODE></b> item. Two orders are equal if their ID is the
         * same.
         * @return the <CODE>boolean</CODE> value of the comparison.
         */
        public boolean equals(Item c) {
            return c.getIdNumber().equals(this.idNumber);
        }

    }

Appliance.java:

package hardwarestore;

public class Appliance extends Item{

    private static final long serialVersionUID = 1L;

    private final String brand;
    private final String type;
    private final String category;


    public Appliance(String idNumber, String name, String brand, String type, String category, int quantity, float price) { 

        super(idNumber, name, brand, quantity, price);

    }



    public String getFormattedInfo() { 

        return null;
    }

}

只需在 class Appliance 中将字段设置为 this

public Appliance(String idNumber, String name, String brand, String type, String category, int quantity, float price) { 

    super(idNumber, name, category, quantity, price);
    this.brand = brand;
    this.type = type;
    this.category = category;
}

顺便说一下,您没有在抽象构造函数中使用类别。