继承的变量不会显示在子类中

时间:2014-08-11 18:52:10

标签: java inheritance

我遇到了未在子类中显示的继承变量的问题。

继承代码......

// Class to instantiate and call

public class UseOrder 
{

    public static void main(String[] args) 
    {
        // Instantiate classes
        Order objOrder=new Order();
        ShippedOrder objShip=new ShippedOrder();
        //Promt for data
        objOrder.SetName();
        objOrder.SetNum();
        objOrder.SetPrice();
        objOrder.SetQuantity();
        // Display
        objShip.Display();
    }

}
//Heres the parent class

public class Order 
{

    //Declare variables
    public String CustName;
    public int CustNum,QuantityOrdered;
    public double Price,TotalPrice;
    //Assign Variables using set methods
    public void SetName()
    {
        this.CustName=JOptionPane.showInputDialog("Enter customer Name");
    }
    public void SetNum()
    {
        this.CustNum=Integer.parseInt(JOptionPane.showInputDialog("Enter customer number"));
    }
    public void SetQuantity()
    {
        this.QuantityOrdered=Integer.parseInt(JOptionPane.showInputDialog("Enter quanntity ordered"));
    }
    public void SetPrice()
    {
        this.Price=Double.parseDouble(JOptionPane.showInputDialog("Enter product price"));
    }
    //Get methods for these variables
    public String GetName()
    {
        return CustName;
    }
    public int GetNum()
    {
        return CustNum;
    }
    public int GetQuantity()
    {
        return QuantityOrdered;
    }
    public double GetPrice()
    {
        return Price;
    }
    //Method to calculate Total Price
    public double ComputePrice()
    {
        return TotalPrice=QuantityOrdered*Price;
    }
    // Method to display
    public void Display()
    {
        JOptionPane.showMessageDialog(null,CustName+" with customer number "+CustNum+" ordered "+QuantityOrdered+" products. \nThe price for one unit is " + Price+" and the total price is "+ComputePrice());
    }
}
 and heres the subclass

public class ShippedOrder extends Order
{

    //Shipping and handling value
    private double ShipFee=4.00;
    // method to override ComputePrice in Order
    public double ComputePrice()
    {
        return super.TotalPrice=(super.QuantityOrdered*super.Price)+ShipFee;
    }
    //Display the data
    public void Display()
    {
        JOptionPane.showMessageDialog(null,super.GetName()+" with customer number "+super.GetNum()+" ordered "+super.GetQuantity()+" products. \nThe price for one unit is " + super.GetPrice()+" and the total price is "+ComputePrice());
    }
}

3 个答案:

答案 0 :(得分:1)

您填充的对象与您正在显示的对象不同,因此难怪您没有获得任何值。您应该将main更改为

public static void main(String[] args) 
{
    // Instantiate classes
    ShippedOrder objShip=new ShippedOrder();
    //Promt for data
    objShip.SetName();
    objShip.SetNum();
    objShip.SetPrice();
    objShip.SetQuantity();
    // Display
    objShip.Display();
}

因为每个ShippedOrder也是Order

答案 1 :(得分:0)

public static void main(String[] args) 
{
    // Instantiate classes
    Order objOrder=new Order();
    ShippedOrder objShip=new ShippedOrder();
    //Promt for data
    objOrder.SetName();
    objOrder.SetNum();
    objOrder.SetPrice();
    objOrder.SetQuantity();
    // Display
    objShip.Display();
}

你永远不会在objShip(派生类)中设置任何东西。所有pre-display()方法调用都在objOrder上,这是一个不同的对象。

也许你的意思是:

public static void main(String[] args) 
{
    // Instantiate classes
    Order objOrder=new ShippedOrder();
    //Promt for data
    objOrder.SetName();
    objOrder.SetNum();
    objOrder.SetPrice();
    objOrder.SetQuantity();
    // Display
    objOrder.Display();
}

答案 2 :(得分:0)

objShip和objOrder是彼此分开的。当您更改订单时,它不会因为它们是不同的对象而更改。如果要从中显示objShip上的方法,则需要在objShip上调用方法。

ShippedOrder objShip = new ShippedOrder();
    objShip.SetName();
    objShip.SetNum();
    objShip.SetPrice();
    objShip.SetQuantity();

    objShip.Display();

并完全删除objOrder。