java - 如何将对象转换为它不继承的类型?

标签 java inheritance

我应该修改一个程序以在 GUI 中呈现输出。除一个字段外,这大部分已完成。我无法显示的唯一字段是“重新进货费用”,它位于 DVD 的子类中。 Inventory 是主类,然后 DVD 类,MovieTitleDVD 的子类。除了 MovieTitle 子类中的 restockingFee 之外,所有内容都显示正确。异常(exception)是:

Exception in thread "main" java.lang.ClassCastException: inventoryProgram3.DVD cannot be cast to inventoryProgram3.MovieTitle
    at inventoryProgram3.Inventory.myRedraw(Inventory.java:204)
    at inventoryProgram3.Inventory.<init>(Inventory.java:149)
    at inventoryProgram3.Inventory.main(Inventory.java:156)  

如何从子类调用 restockfee 方法?

这是Inventory 类:

package inventoryProgram4;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Inventory extends JFrame implements ActionListener {
    // declare instance attributes
    private final JPanel buttonJPanel;
    private final JPanel areaJPanel;
    private final JButton buttons[];
    private final BorderLayout layout;
    private final Container pane;
    private final JLabel l0;
    private final JLabel l1;
    private final JLabel l2;
    private final JLabel l3;

    private final JLabel l4;
    private final JLabel l5;
    private final JLabel totalInv;

    // Text fields
    static private JTextField t0;
    static private JTextField t1;
    static private JTextField t2;
    static private JTextField t3;
    static private JTextField t4;
    static private JTextField t5;

    static private JTextField totalInvValue;
    static private DVD rb[];
    static private int index;

    static private DVD[] DVD;

    public Inventory() { // constructor
        super("DVD Inventory");

        DVD = new DVD[5];
        DVD[0] = new DVD("1", " Fast and Furious 6", 10, 15);
        DVD[1] = new DVD("2", " The Matrix Reloaded", 8, 5);
        DVD[2] = new DVD("3", " In Pursuit of Happiness", 8, 5);
        DVD[3] = new DVD("4", " Darknet", 8, 5);
        DVD[4] = new DVD("5", " Goonies", 8, 5);

        DVD = sortDVD(DVD);

        rb = DVD;
        index = 0;

        // setting layout
        layout = new BorderLayout(25, 15);
        setLayout(layout);

        // getting pane
        pane = getContentPane();

        // set up areaJPanel
        areaJPanel = new JPanel();
        areaJPanel.setLayout(new GridLayout(0, 2));

        // create area components
        l0 = new JLabel("   DVD Title:   ");
        l1 = new JLabel("   Title Name: ");
        l2 = new JLabel("   DVD's in Stock:    ");
        l3 = new JLabel("   DVD Price:  ");

        l4 = new JLabel("   Restock Fee: ");
        l5 = new JLabel("   Inventory value: ");
        totalInv = new JLabel("   Value of entire inventory: ");
        totalInvValue = new JTextField(10);
        totalInvValue.setEditable(false);

        t0 = new JTextField(15);
        t0.setEditable(false); // making text box non editable
        t1 = new JTextField(15);
        t1.setEditable(false);
        t2 = new JTextField(10);
        t2.setEditable(false);
        t3 = new JTextField(10);
        t3.setEditable(false);
        t4 = new JTextField(10);
        t4.setEditable(false);
        t5 = new JTextField(10);
        t5.setEditable(false);

        // adding components
        areaJPanel.add(l0); // n x 2 grid: label - textfield
        areaJPanel.add(t0);
        areaJPanel.add(l1);
        areaJPanel.add(t1);
        areaJPanel.add(l2);
        areaJPanel.add(t2);
        areaJPanel.add(l3);
        areaJPanel.add(t3);

        areaJPanel.add(t4);
        areaJPanel.add(l4);
        areaJPanel.add(t4);
        areaJPanel.add(l5);
        areaJPanel.add(t5);
        areaJPanel.add(totalInv);
        areaJPanel.add(totalInvValue);

        // Adding four buttons: first, next, previous, last
        // create array of buttons, size 4
        buttons = new JButton[4];

        // create a JPanel for the buttons
        buttonJPanel = new JPanel();
        // set panel layout to 1 row and columns = size of array
        buttonJPanel.setLayout(new GridLayout(1, buttons.length));

        // adding button "next"
        buttons[1] = new JButton("Previous"); // instantiate button "next"
        buttons[1].setActionCommand("Previous"); // identifies the action event
        buttons[1].addActionListener(this); // add an action listener for button
        buttonJPanel.add(buttons[1]); // add button to the button JPanel

        // adding button "previous"
        buttons[2] = new JButton("Next"); // instantiate button "previous"
        buttons[2].setActionCommand("next"); // identifies the action event
        buttons[2].addActionListener(this); // add an action listener for button
        buttonJPanel.add(buttons[2]); // add button to the button JPanel

        pane.add(buttonJPanel, BorderLayout.SOUTH); // place buttons at bottom of pane
        pane.add(areaJPanel, BorderLayout.WEST);

        myRedraw(); // showing first item

    }// end constructor

    public static void main(String args[]) {

        // creating frame
        Inventory panelFrame = new Inventory();
        // close the frame on clicking X button
        panelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // setting size
        panelFrame.pack();
        // setting visibility
        panelFrame.setVisible(true);

    }// end main method

    static DVD[] sortDVD(DVD[] DVD) {
        int in, out, NumOfEle = DVD.length;

        for (out = 1; out < NumOfEle; out++) {
            DVD temp = DVD[out];
            in = out;

            while (in > 0 && DVD[in - 1].getTitle().compareTo(temp.getTitle()) > 0) {
                DVD[in] = DVD[in - 1];
                --in;
            }
            DVD[in] = temp;
        }
        return DVD;
    }

    private static double calculateEntireInventory(DVD[] DVD1) {// method to return inventory total
        double totalInventory = 0;
        for (DVD DVD : DVD1) {
            totalInventory += (DVD.getInventoryValue());
        }
        return totalInventory;
    }

    // method to display contents
    static void myRedraw() {
        // currency formatter
        NumberFormat nf = new DecimalFormat("$#,##0.00");
        // reference to object under consideration
        DVD f = (DVD) rb[index];
        double inventoryValue = f.getInventoryValue();
        // set values for indicated object attribute
        t0.setText(f.getTitle()); // item name
        t1.setText(f.getItem() + ""); // item number
        t2.setText(f.getStock() + ""); // item quantity
        t3.setText(nf.format(f.getPrice())); // item price

        // Problem here
        /**
         * Exception in thread "main" java.lang.ClassCastException: inventoryProgram3.DVD cannot be
         * cast to inventoryProgram3.MovieTitle at
         * inventoryProgram3.Inventory.myRedraw(Inventory.java:204) at
         * inventoryProgram3.Inventory.<init>(Inventory.java:149) at
         * inventoryProgram3.Inventory.main(Inventory.java:156)
         **/

        MovieTitle t = (MovieTitle) f;

        t4.setText(nf.format(t.getRestockingFee())); // item re-stocking fee

        // up above

        t5.setText(nf.format(inventoryValue));

        // up above

        // total value of inventory
        totalInvValue.setText(nf.format(calculateEntireInventory(DVD)));
    }// end method

    // action handler for button presses
    public void actionPerformed(ActionEvent event) {
        int last = rb.length - 1;

        // get command string assigned to the pressed button.
        String action = event.getActionCommand();

        // set index based on which button was pressed.
        if (action.equals("prev")) { // previous pressed
            if (index == 0) {
                index = last; // at first item, go to last item
            } else {
                --index; // go to previous item
            }
        } else {// next pressed
            if (index == last) {
                index = 0; // at last item, go to first time
            } else {
                ++index; // go to next item
            }
        }
        myRedraw();

    }// end method

} // end program

这是 DVD 类:

package inventoryProgram3;

class DVD {
    // Create local variable to capture data from parameter list of the constructor

    private String thisItem;
    private String thisTitle;
    private double thisStock;
    private double thisPrice;

    // Create constructor to call this class from external classes
    public DVD(String item, String title, double stock, double price) {
        // Set local variables to values passed in via a class call
        thisItem = item;
        thisTitle = title;
        thisStock = stock;
        thisPrice = price;
    } // end constructor

    // getInventoryValue: calculates the total value of the inventory for the item
    public double getInventoryValue() {
        return getPrice() * getStock();
    }

    // set name
    public void setTitle(String title) {
        thisTitle = title;
    }// end method setTitle

    // return Title
    public String getTitle() {
        return thisTitle;
    }// end method getTitle

    // set Stock
    public void setStock(double stock) {
        thisStock = stock;
    }// end method setStock

    // return Stock
    public double getStock() {
        return thisStock;
    }// end method get Stock

    public void setPrice(double price) {
        thisPrice = price;
    }// end method setPrice

    // return Price
    public double getPrice() {
        return thisPrice;
    }// end method getPrice

    public void setItem(String item) {
        thisItem = item;
    }// end method setItem

    // return Item
    public String getItem() {
        return thisItem;
    }// end method getItem

    // calculate the inventory value
    public double value() {
        return thisPrice * thisStock;
    }// end method value
}// end class

这是 MovieTitle 类:

package inventoryProgram3;

public class MovieTitle extends DVD {
    private final double restockingFee = 0.05;

    public MovieTitle(String item, String title, double stock, double price) {
        super(item, title, stock, price);
    }

    public double getRestockingFee() {
        return getPrice() * restockingFee;
    }
}

最佳答案

您不能将父类(super class)对象强制转换为其子对象之一,只允许相反的操作。看起来修复当前问题的最简单方法是将数组声明为 child 类型,并在主程序中用子对象填充它,然后您就可以访问子类和父类(super class)方法。

关于java - 如何将对象转换为它不继承的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31091066/

相关文章:

java - 在android代码中没有得到web服务的响应

java - 如何使用具有多个连接的 SQL 查询并使用 hibernate 计数

java - 从资源中读取json文件,并将其转换为JAVA中的json字符串

c++ - 使用 "interface"时没有在类中声明成员函数

JAVA:类型参数隐藏类类型

inheritance - Modelica - 'extends' 带下拉菜单

java - 谁能解释为什么 C 和 Java 的输出不同?

java - 创建 LDAP 领域时出现 Glassfish 错误 : "Invalid property syntax, "="in value

java - 无法编译已实现接口(interface)的抽象类

php - 为什么方法声明不兼容?