下面是我写的一段程序,有错误,请高手指教(很简单)
public class PClass
{
private int pNumber;
private double pPrice;
PClass(){}
PClass(int number,double price)
{
pNumber=number;
pPrice=price;
}
public void showNumber()
{
System.out.println(pNumber);
}
public void showPrice()
{
System.out.println(pPrice);
}
public static void main(String[] args)
{
PClass p=new PClass(1,100.0);
p.showNumber();
p.showPrice();
}
}
public class WClass extends PClass
{
private int pStage;
WClass(){}
WClass(int number,double price,int stage)
{
pNumber=number;
pPrice=price;
pStage=stage;
}
public void showStage()
{
System.out.println(pStage);
}
public static void main(String[] args)
{
WClass w=new WClass(1,1000.0,2);
w.showNumber();
w.showPrice();
w.showStage();
}
}
------解决方案--------------------private的属性只能在类内部访问
改成 public , protected ,或者default 都可以
public int pNumber;
public double pPrice;