Java 继承的变量未显示在子类中
我有一个问题,继承的变量没有显示在子类中 这是密码Java 继承的变量未显示在子类中,java,inheritance,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()
// 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());
}
}
填充的对象与显示的对象不同,因此难怪没有值。你应该把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();
}
因为每个发货订单
也是一个订单
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();
}
objShip和objOrder是相互独立的。当您更改订单时,它不会更改装运,因为它们是不同的对象。在这种情况下,如果希望从中显示,则需要调用objShip上的方法
ShippedOrder objShip = new ShippedOrder();
objShip.SetName();
objShip.SetNum();
objShip.SetPrice();
objShip.SetQuantity();
objShip.Display();
我现在明白我做错了什么,非常感谢你的帮助:)我明白我做错了什么,谢谢你的回答:]我现在明白我做错了什么,谢谢你的快速回答:]