在从抽象类扩展的类中分配新变量

时间:2018-02-18 01:59:02

标签: java abstract-class abstract

我有一个名为items的抽象类,我想创建两个名为Appliance和SmallHwItem的类来扩展items类。在我的项目抽象类中,我有两个扩展它的类是常量的变量,但是想要在设备和smallhwitem类中的items构造函数中添加不同的变量。例如:Id喜欢包含字符串变量" Type"," Brand"和" Category"在设备类中。对不起,我是抽象课程的新手,非常感谢任何反馈。以下是我的两个课程。

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;
    }

}

1 个答案:

答案 0 :(得分:0)

只需使用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;
}

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